Codeforces Round 949 (Div. 2) C. Turtle and an Incomplete Sequence 题解 构造

Turtle and an Incomplete Sequence

题目描述

Turtle was playing with a sequence a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an consisting of positive integers. Unfortunately, some of the integers went missing while playing.

Now the sequence becomes incomplete. There may exist an arbitrary number of indices i i i such that a i a_i ai becomes − 1 -1 1. Let the new sequence be a ′ a' a.

Turtle is sad. But Turtle remembers that for every integer i i i from 1 1 1 to n − 1 n - 1 n1, either a i = ⌊ a i + 1 2 ⌋ a_i = \left\lfloor\frac{a_{i + 1}}{2}\right\rfloor ai=2ai+1 or a i + 1 = ⌊ a i 2 ⌋ a_{i + 1} = \left\lfloor\frac{a_i}{2}\right\rfloor ai+1=2ai holds for the original sequence a a a.

Turtle wants you to help him complete the sequence. But sometimes Turtle makes mistakes, so you need to tell him if you can’t complete the sequence.

Formally, you need to find another sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn consisting of positive integers such that:

  • For every integer i i i from 1 1 1 to n n n, if a i ′ ≠ − 1 a'_i \ne -1 ai=1, then b i = a i ′ b_i = a'_i bi=ai.
  • For every integer i i i from 1 1 1 to n − 1 n - 1 n1, either b i = ⌊ b i + 1 2 ⌋ b_i = \left\lfloor\frac{b_{i + 1}}{2}\right\rfloor bi=2bi+1 or b i + 1 = ⌊ b i 2 ⌋ b_{i + 1} = \left\lfloor\frac{b_i}{2}\right\rfloor bi+1=2bi holds.
  • For every integer i i i from 1 1 1 to n n n, 1 ≤ b i ≤ 1 0 9 1 \le b_i \le 10^9 1bi109.

If there is no sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn that satisfies all of the conditions above, you need to report − 1 -1 1.

输入描述

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 5 1 \le t \le 10^5 1t105). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105) — the length of the sequence.

The second line of each test case contains n n n integers a 1 ′ , a 2 ′ , … , a n ′ a'_1, a'_2, \ldots, a'_n a1,a2,,an ( a i ′ = − 1 a'_i = -1 ai=1 or 1 ≤ a i ′ ≤ 1 0 8 1 \le a'_i \le 10^8 1ai108) — the elements of the sequence a ′ a' a.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输出描述

For each test case, if there is no sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn that satisfies all of the conditions, output a single integer − 1 -1 1.

Otherwise, output n n n integers b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn — the elements of the sequence b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,,bn you find. The sequence should satisfy that 1 ≤ b i ≤ 1 0 9 1 \le b_i \le 10^9 1bi109 for every integer i i i from 1 1 1 to n n n. If there are multiple answers, print any of them.

样例 #1

样例输入 #1

9
8
-1 -1 -1 2 -1 -1 1 -1
4
-1 -1 -1 -1
6
3 -1 -1 -1 9 -1
4
-1 5 -1 6
4
2 -1 -1 3
4
1 2 3 4
2
4 2
5
-1 3 -1 3 6
13
-1 -1 3 -1 -1 -1 -1 7 -1 -1 3 -1 -1

样例输出 #1

4 9 4 2 4 2 1 2
7 3 6 13
3 1 2 4 9 18
-1
-1
-1
4 2
6 3 1 3 6
3 1 3 1 3 7 3 7 3 1 3 1 3

原题

CF——传送门

思路&代码

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
typedef long long ll;void solve()
{int n;cin >> n;vector<int> a(n);vector<int> idx;for (int i = 0; i < n; i++){cin >> a[i];if (a[i] != -1)idx.push_back(i); // idx数组保存所有值非-1的下标}if (idx.empty()) // 特判全是-1的情况{for (int i = 0; i < n; i++) // 这种情况只需要1和2交替出现即可{if (i & 1)a[i] = 1;elsea[i] = 2;}for (int i = 0; i < n; i++)cout << a[i] << " \n"[i == n - 1];}else{auto path = [&](int x, int y) -> vector<int>{vector<int> left, right; // left和right分别为x到lca的路径和y到lca的路径// 先将x和y转移到二叉树的同一层中while ((int)log2(x) > (int)log2(y)){left.push_back(x);x >>= 1;}while ((int)log2(y) > (int)log2(x)){right.push_back(y);y >>= 1;}// 然后x和y同时向LCA转移while (x != y){left.push_back(x);right.push_back(y);x >>= 1;y >>= 1;}// 勿忘加入LCAleft.push_back(x);// 合并left和right两个数组for (int i = right.size() - 1; i >= 0; i--){left.push_back(right[i]);}// 返回合并后的数组,即路径return left;};// 第一个值非-1的索引左侧的-1可以通过上一个值交替乘2,除2来构造for (int i = idx[0] - 1, cnt = 1; i >= 0; i--, cnt++){if (cnt & 1)a[i] = a[i + 1] * 2;elsea[i] = a[i + 1] / 2;}// 最后一个值非-1的索引右侧的-1可以通过上一个值交替乘2,除2来构造for (int i = idx[idx.size() - 1] + 1, cnt = 1; i < n; i++, cnt++){if (cnt & 1)a[i] = a[i - 1] * 2;elsea[i] = a[i - 1] / 2;}// 对于两个值非-1的索引中间的-1,找到值a[idx[i]]和a[idx[i+1]]的LCA(最近公共祖先),然后存储a[idx[i]]到a[idx[i+1]]的转移路径for (int i = 0; i < idx.size() - 1; i++){int x = idx[i];int y = idx[i + 1];vector<int> res = path(a[x], a[y]);// 1.如果路径大小的奇偶性和数组内区间元素个数的奇偶性不同,则无法构造(因为若有剩余-1,需要交替*2,/2,而这个操作一定是偶数次)// 2.如果路径大小大于数组内区间元素个数,则无法构造,因为-1的数量不足够实现a[idx[i]]到a[idx[i+1]]的转移if (((res.size() & 1) ^ ((y - x + 1) & 1)) || (res.size() > (y - x + 1))){cout << -1 << '\n';return;}// 如果可以实现构造,先将res路径更新到a数组中for (int j = x, cnt = 0; j < x + res.size(); j++, cnt++){a[j] = res[cnt];}// 然后通过交替*2,/2的操作来填补剩余的-1(剩余偶数个-1)for (int j = x + res.size(), cnt = 1; j <= y; j++, cnt++){if (cnt & 1)a[j] = a[j - 1] * 2;elsea[j] = a[j - 1] / 2;}}for (int i = 0; i < n; i++)cout << a[i] << " \n"[i == n - 1];}
}int main()
{ios::sync_with_stdio(0);cin.tie(0);int t;cin >> t;while (t--){solve();}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/22047.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

解决Chat打开时Unable to load conversation 的问题

在开梯子的情况下打开chat依然很卡&#xff0c;这里选择edge的浏览器无痕模式&#xff08;新建InPrivate窗口&#xff09;&#xff0c;在无痕窗口下打开chat就可以了。

python ---requests

python包管理工具 pip 若发现报错&#xff0c;则可以通过 -i 命令指定软件源 requests库安装 通过 pip &#xff0c;如上 或通过 pycharm 搜索 requests &#xff0c;并安装即可 下载成功的证明 requests库使用 模拟 http 重要参数如下 如何模拟发包 支持模拟各种 http meth…

Redis 集群:主从复制配置指南

Redis 主从集群配置 首先&#xff0c;确保你的系统上已安装 Redis。接下来&#xff0c;我们将配置一个简单的 Redis 主从集群。 1. 配置主节点 编辑主节点的 redis.conf 文件&#xff0c;通常位于 /etc/redis/ 目录下。配置持久化和密码&#xff08;如果需要&#xff09;&am…

SpringMVC:消息转换器

1. HttpMessageConvertor 简介 HttpMessageConverter是Spring MVC中非常重要的一个接口。翻译为&#xff1a;HTTP消息转换器。该接口下提供了很多实现类&#xff0c;不同的实现类有不同的转换方式。 转换器 如上图所示&#xff1a;HttpMessageConverter接口的可以将请求协议转…

基于ESP32-S3芯片的通用型无线模组方案,启明云端乐鑫一级代理商

随着物联网技术的飞速发展&#xff0c;智能设备正以前所未有的速度进入到我们的日常生活中&#xff0c;AIoT&#xff08;人工智能物联网&#xff09;已成为智能家居、智能设备、智能安防等领域的核心技术。 作为乐鑫一级代理商&#xff0c;基于ESP32-S3芯片&#xff0c;启明云…

科技云报道:走出“实验室”,GenAI迎来关键拐点

科技云报道原创。 对传统产业来说&#xff0c;GenAI是一场“哥白尼式的革命”&#xff0c;它改变了传统的业务模式&#xff0c;开启了人类与AI合作的新纪元。基于AI助手和大语言模型&#xff0c;企业能够实现智能运营的目标。 如果说&#xff0c;2022年是AI大模型元年&#x…

【全开源】Java AI绘画MJ绘画源码小程序APP公众号源码AI绘图

&#x1f3a8; 探索AI绘画的奥秘 一、引言&#xff1a;AI绘画的魅力 &#x1f308; 在这个数字化飞速发展的时代&#xff0c;AI绘画已经不再是遥不可及的梦想。通过源码小程序&#xff0c;我们可以轻松探索AI绘画的奥秘&#xff0c;感受科技与艺术的完美结合。今天&#xff0…

电脑误删除文件如何恢复?几种常用的数据恢复方法分享!

处理电脑文件时误删是大部分电脑用户可能都会面临的一个问题。如果是比较重要的文件&#xff0c;很多用户就会开始心慌&#xff0c;不知道如何是好。那么&#xff0c;电脑怎么恢复删除的文件呢&#xff1f; 其实方法很简单&#xff0c;下面小编就给大家分享几种常用的数据恢复方…

react-学习基础偏

1.新建文件夹 2.vscode引入这个文件夹 3.打开vscode终端 执行命令 npx create-react-app react-basic 创建基本项目&#xff08;react-basic项目文件夹名&#xff09; 4.进入到这个文件夹 可用的一些命令 这就算启动成功 5. 这是项目的核心包 渲染流程

BLE芯片DA145XX系列:HOGP功能实现

DA145XX协议芯片支持配置HOGP功能。即模拟蓝牙键鼠之类的设备&#xff0c;实现和手机绑定后&#xff0c;靠近设备手机自动回连设备的功能。实现HOGP功能需要对默认SDK做特殊配置&#xff0c;具体流程如下&#xff1a;1、配置宏定义 da1458x_config_basic.h文件&#xff1a; /*…

java自学阶段二:JavaWeb开发06(mybatis学习)

目录&#xff1a; 学习目标mybatis的基础用法&#xff08;新增、删除、修改、查询&#xff09; 一&#xff1a;学习目标&#xff1a; 1&#xff09;了解mybatis的基础概念&#xff1b; 2&#xff09;学会mybatis的基础用法&#xff1b; 二、mybatis的基础概念&#xff1a; M…

手把手教你改进YOLOv8小目标检测(多尺度特征融合iAFF)

1,YOLOv8改进策略指南 YOLOv8是目标检测领域中一个重要的模型,它在YOLO系列的基础上进行了进一步的改进和优化。 根据搜索结果,YOLOv8的一些改进策略包括: 注意力机制的增加:通过引入注意力机制,可以提高模型对目标特征的捕捉能力,从而提升检测性能2369。 卷积和Block的…

Unity Obi Rope失效

文章目录 前言一、WebGL端Obi Rope失效二、Obi Rope 固定不牢三、使用Obi后卡顿总结 前言 Obi 是一款基于粒子的高级物理引擎&#xff0c;可模拟各种可变形材料的行为。 使用 Obi Rope&#xff0c;你可以在几秒内创建绳索和杆子&#xff0c;同时完全控制它们的形状和行为&…

scipy.io.loadmat加载.mat文件,出现KeyError: ‘xxx‘

源代码&#xff1a; input_image loadmat(rC:\Users\admin\Downloads\Indian_Pines\SVM/aa.mat)[aa] #影像图 错误显示&#xff1a; 解决方法&#xff1a; 因为loadmat函数读取出来的高光谱数据是dict格式的所以需要定位才能进行后续操作&#xff0c;定位通常是通过列名&a…

运筹说 第116期 | 算法介绍之排队论

在这个快节奏的时代&#xff0c;无论是线上购物、线下服务&#xff0c;还是工业生产&#xff0c;我们都不可避免地与“排队”打交道。今天小编将带你一起探索利用Python和MATLAB这两种编程工具&#xff0c;来求解排队论中的常见模型和排队优化问题。我们将从排队论的基础模型开…

U盘杀毒是否会导致文件丢失?误删文件如何恢复?

在数字化时代&#xff0c;U盘作为便携的数据存储设备&#xff0c;广泛应用于我们的日常生活与工作中。然而&#xff0c;随着网络环境的复杂化&#xff0c;U盘也时常成为病毒传播的媒介。因此&#xff0c;对U盘进行杀毒成为保护数据安全的重要步骤。但许多用户担心&#xff0c;给…

centos安装vscode的教程

centos安装vscode的教程 步骤一&#xff1a;打开vscode官网找到历史版本 历史版本链接 步骤二&#xff1a;找到文件下载的位置 在命令行中输入&#xff08;稍等片刻即可打开&#xff09;&#xff1a; /usr/share/code/bin/code关闭vscode后&#xff0c;可在应用程序----编程…

Java 线程间信息传递方式

方法一&#xff1a; 将共享数据定义为静态类变量 适合全局共享数据的场景&#xff0c;即进程内所有线程可访问。 将数据对象定义为静态类变量&#xff0c;进程内的所有线程即可在数据对象允许的范围内实现访问数据对象。 方法二&#xff1a; 使用volatile关键字在线程之间共享…

浏览器是如何解析CSS选择器的

在生成渲染树的过程中&#xff0c;渲染引擎会根据选择器提供的信息来遍历 DOM 树&#xff0c;找到对应的 DOM 节点后将样式规则附加到上面。 来看一段样式选择器代码、以及一段要应用样式的 HTML&#xff1a; css: .mod-nav h3 span {font-size: 16px; } html: <div c…

10Linux 进程管理学习笔记

Linux 进程管理 目录 文章目录 Linux 进程管理一.进程1.显示当前进程状态(ps)进程树(pstree)1.1实时显示进程信息(top)顶部概览信息&#xff1a;CPU 状态&#xff1a;内存状态&#xff1a;进程信息表头&#xff1a;进程列表&#xff1a;1.2(htop) 2.终止进程(kill)2.1通过名称…