CF1023D Array Restoration
思路:
使用set即可,细节很多,容易出错。
实现:
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
int a[MAXN], l[MAXN], r[MAXN];
int main()
{
int n, q;
while (cin >> n >> q)
{
for (int i = ; i <= q; i++) { l[i] = INF; r[i] = -INF; }
int c0 = , cq = ;
for (int i = ; i <= n; i++)
{
cin >> a[i];
c0 += a[i] == ;
cq += a[i] == q;
if (a[i] == ) continue;
l[a[i]] = min(l[a[i]], i);
r[a[i]] = max(r[a[i]], i);
}
if (!c0 && !cq) { cout << "NO" << endl; continue; }
set<int> st;
bool flg = true;
for (int i = ; i <= n; i++)
{
if (i == l[a[i]]) st.insert(a[i]);
if (a[i] && !st.empty() && *st.rbegin() != a[i]) { flg = false; break; }
int tmp = -;
if (a[i] == )
{
if (cq == ) { tmp = q; cq++; }
else if (!st.empty()) tmp = *st.rbegin();
else tmp = ;
}
if (i == r[a[i]]) st.erase(st.find(a[i]));
if (tmp != -) a[i] = tmp;
}
if (!cq || !flg) { cout << "NO" << endl; continue; }
cout << "YES" << endl;
for (int i = ; i <= n; i++) cout << a[i] << " ";
cout << endl;
}
return ;
}
CF1023D Array Restoration的更多相关文章
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- codeforces 1023 D. Array Restoration 并查集
D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #504:D. Array Restoration
D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...
- 【CF1023D】Array Restoration(构造,线段树)
题意:有一个长为n的序列,对其进行q次操作,第i次操作可以把连续的一段覆盖为i 现在给出操作后的序列,第i个数字为a[i],其中有一些为0的位置可以为任意值,要求构造任意一组合法的操作后的序列 无解输 ...
- CF 1023D Array Restoration - 线段树
题解 非常容易想到的线段树, 还可以用并查集来. 还有一位大神用了$O(n)$ 就过了Orz 要判断是否能染色出输入给出的序列,必须满足两个条件: 1. 序列中必须存在一个$q$ 2. 两个相同的数$ ...
- Codeforces #504(div1+div2) 1023D Array Restoration(线段树)
题目大意:给你一个数组,数组是经过q次区间覆盖后的结果,第i次覆盖是把区间内的值赋值为i,其中有若干个地方数值未知(就是0),让你判断这个数组是否可以经过覆盖后得到的,如果可以,输出任意一种可行数组. ...
- Codeforces Round #667 (Div. 3) C. Yet Another Array Restoration (数学)
题意:给你两个数字\(x\)和\(y\),让你构造一个长度为\(n\)的序列,要求包含\(x\)和\(y\),并且排序后相邻两项的差值相等. 题解:有排序后相邻两项的差值相等可知,构造的序列排序后一定 ...
- Codeforces 1023 D.Array Restoration-RMQ(ST)区间查询最值 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)
D. Array Restoration 这题想一下就会发现是只要两个相同的数之间没有比它小的就可以,就是保存一下数第一次出现和最后一次出现的位置,然后查询一下这个区间就可以,如果有0的话就进行填充. ...
- Codeforces1409 题解(A-F)
A. Yet Another Two Integers Problem 最优的操作中,\(k = \min(10, abs(a - b))\),记\(d=abs(a-b)\),最终的答案为\(ans ...
随机推荐
- Microsoft visual studio关闭安全检查
在用Microsoft visual studio进行代码编写时,使用到列如sprintlf这种比较旧的指令,需要关闭Microsoft visual studio的安全检查: 设置预处理选项:a. ...
- python-pycharm 设置默认代码及注释
pycharm
- 微信小程序内嵌网页能力开放 小程序支持内嵌网页文档说明
为了方便开发者灵活配置微信小程序,张小龙现在开放了小程序的内嵌网页功能,这是一个非常大的惊喜啊,以后意味着你只要开放一个手机端网站,就可以制作一个小程序了哦.操作方法1.开发者登录微信小程序后台,选择 ...
- 【旧文章搬运】KeUserModeCallback用法详解
原文发表于百度空间及看雪论坛,2010-01-10 看雪论坛地址:https://bbs.pediy.com/thread-104918.htm 代码及附件可到这里下载=============== ...
- C杂谈
最近在做关于C的项目开发,记录一下有关C的操作,比较杂乱 1.利用System进行文件数量统计: 1) system("dir /b /s /ad d:\\mydir\\*.* | find ...
- http://www.cnblogs.com/dasenglin/p/5821987.html
一安装maven 先安装jdk,配置JAVA_HOME 把下载的maven bin包,解压到指定目录,比如:D:\apache-maven-3.3.9-bin 配置maven的系统变量M2_HOME和 ...
- nodejs mac启动相关命令
redis: redis directory/src/redis-server redis-directory/redis.conf & mongodb: mongod &
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- 解读大内老A的《.NET Core框架本质》
老A说的一句话让我很受启发,想要深入了解框架,你要把精力聚焦在架构设计的层面来思考问题.而透彻了解底层原理,最好的笨办法就是根据原理对框架核心进行重建或者说再造.看起来没有捷径,也是最快的捷径. 题外 ...
- setInterval和setTImeout中的this指向问题
问题:在setInterval和setTimeout中传入函数时,函数中的this会指向window对象 解决方法: 1. 将当前对象的this存为一个变量,定时器内的函数利用闭包来访问这个变量.va ...