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 ...
随机推荐
- bzoj2957楼房重建——线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2957 线段树维护原点到楼顶的斜率,可以知道答案就是从原点开始斜率递增的个数: 记录一个mx数 ...
- bzoj2959
lct+并查集 联赛之后忘了很多东西 复习一下 这并不是一棵树,所以我们不能直接上lct 但是把双联通分量缩了以后就是一棵树了 怎么缩呢 就是把splay拆了合并到一个点上 连通性和双联通分量拿两个并 ...
- sort()的升降序函数操作
sort()函数包含在头文件#include <algorithm>中,是常用的排序函数. functional提供了一堆基于模板的比较函数对象.它们是:equal_to<Type& ...
- TypeScript完全解读(26课时)_3.TypeScript完全解读-Symbol
ts中symbol的支持是按照es6的标准来的,只要我们学会es6中的symbol,就可以直接在ts中使用了 创建symbol 在example文件夹下新建symbol.ts 然后在根目录的index ...
- node代理网络请求
server.js var express = require('express') var axios = require('axios') var port = 9000; var app = e ...
- iOS 中 延迟操作四种方式
本文列举了四种延时执行某函数的方法及其一些区别.假如延时1秒时间执行下面的方法. - (void)delayMethod { NSLog(@"execute"); } 1.perf ...
- C++构造函数和拷贝构造函数详解
构造函数.析构函数与赋值函数是每个类最基本的函数.它们太普通以致让人容易麻痹大意,其实这些貌似简单的函数就象没有顶盖的下水道那样危险. 每个类只有一个析构函数和一个赋值函数,但可以有多个构造函数(包含 ...
- ZOJ3321,ZOJ3317
ZOJ3321 //there is at most one edge between two nodes. 因为这句话的局限性,又要满足环,那么一定是每个点度为2,然后为n节点的一个环 //#inc ...
- UVALive 7327【模拟】
题意: 每次方案一个或多个子序列: 每个子序列要整除m 认为分割不同,子序列边界的不同就是不同: 1246有4个 1246 12 46 124 6 12 4 6 思路: 先从整体考虑,因为取膜适用于加 ...
- laravel 视图调用方法并传递参数
视图层 route 中文 路由 <a href="{{route('cc',array('id'=>11111))}}">446454</a> 路由层 ...