D. Array Restoration

这题想一下就会发现是只要两个相同的数之间没有比它小的就可以,就是保存一下数第一次出现和最后一次出现的位置,然后查询一下这个区间就可以,如果有0的话就进行填充。

这个题我是用RMQ(ST)进行查询的,在初始化的时候,如果有0就把0变成2e5+1,因为数据最大2e5,然后区间查询就可以了,关于RMQ(ST),以前写过博客,有兴趣的可以看一下

RMQ(ST)详解

这个题坑点很多,首先如果这个序列一开始的最大值比m小,并且没有0,那么就是NO,有0就先把一个0变成m,然后填充的时候,如果一开始的最大值就是0,说明全部为0,那么直接把所有0变成m然后输出就可以了。其他的具体的看代码吧,写了注释。

代码:

  1 //D-区间查询最值,RMQ查询
2 #include<iostream>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<bitset>
7 #include<cassert>
8 #include<cctype>
9 #include<cmath>
10 #include<cstdlib>
11 #include<ctime>
12 #include<deque>
13 #include<iomanip>
14 #include<list>
15 #include<map>
16 #include<queue>
17 #include<set>
18 #include<stack>
19 #include<vector>
20 using namespace std;
21 typedef long long ll;
22
23 const double PI=acos(-1.0);
24 const double eps=1e-6;
25 const ll mod=1e9+7;
26 const int inf=0x3f3f3f3f;
27 const int maxn=2*1e5+10;
28 const int maxm=100+10;
29 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
30
31 int a[maxn],mm[maxn][maxm],mi[maxn][maxm];
32 int n,m,MIN,MAX;
33
34 void ST()
35 {
36 for(int i=1;i<=n;i++){
37 if(a[i]!=0)
38 mm[i][0]=mi[i][0]=a[i];
39 else
40 mm[i][0]=mi[i][0]=2*1e5+1;//把0变成最大的
41 }
42 for(int j=1;(1<<j)<=n;j++){
43 for(int i=1;i+(1<<j)-1<=n;i++){
44 mm[i][j]=max(mm[i][j-1],mm[i+(1<<(j-1))][j-1]);
45 mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
46 }
47 }
48 }
49
50 void RMQ(int l,int r)
51 {
52 int k=0;
53 while((1<<(k+1))<=r-l+1)k++;
54 MAX=max(mm[l][k],mm[r-(1<<k)+1][k]);
55 MIN=min(mi[l][k],mi[r-(1<<k)+1][k]);
56 }
57
58 vector<int> vec[maxn];//保存数出现的所有位置
59
60 int main()
61 {
62 cin>>n>>m;
63 int flag=0,maxx=-1,pos=0;
64 for(int i=1;i<=n;i++){
65 cin>>a[i];
66 maxx=max(maxx,a[i]);
67 if(a[i]==0)pos=i;
68 vec[a[i]].push_back(i);
69 }
70 if(maxx<m&&maxx>0&&pos!=0) a[pos]=m;//如果一开始所有数都小于m,就把一个0变成m
71 else if(maxx==0){//如果所有数都为0,直接全变成0然后输出就可以了
72 for(int i=1;i<=n;i++)
73 a[i]=m;
74 cout<<"YES"<<endl;
75 for(int i=1;i<=n-1;i++)
76 cout<<a[i]<<" ";
77 cout<<a[n]<<endl;
78 return 0;
79 }
80 else if(maxx<m&&pos==0){//如果最大值小于m并且没有0,就是NO
81 cout<<"NO"<<endl;
82 return 0;
83 }
84 ST();
85 for(int i=1;i<=m;i++){
86 if(vec[i].size()!=0){
87 int l=vec[i][0];//值为i的数第一次出现的位置
88 int r=vec[i][vec[i].size()-1];//最后一次出现的位置
89 RMQ(l,r);
90 if(MIN<i){//查询区间的最小值
91 flag=1;
92 break;
93 }
94 }
95 }
96 if(flag==1){
97 cout<<"NO"<<endl;
98 return 0;
99 }
100 for(int i=1;i<=n;i++){
101 if(a[i]==0){
102 for(int j=i+1;j<=n;j++){//从当前位置找第一个不为0的数
103 if(a[j]==0) continue;
104 else{
105 a[i]=a[j];
106 break;
107 }
108 }
109 if(a[i]==0) a[i]=a[i-1];//如果右端点全为0,就填充左边的数
110 }
111 }
112 cout<<"YES"<<endl;
113 for(int i=1;i<=n-1;i++)
114 cout<<a[i]<<" ";
115 cout<<a[n]<<endl;
116 }

嘤嘤嘤,溜了。

Codeforces 1023 D.Array Restoration-RMQ(ST)区间查询最值 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)的更多相关文章

  1. Codeforces 1023 A.Single Wildcard Pattern Matching-匹配字符 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)

    Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter ...

  2. Codeforces 1023 C.Bracket Subsequence-STL(vector) (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)

    C. Bracket Subsequence ... 代码: 1 //C 2 #include<iostream> 3 #include<cstdio> 4 #include& ...

  3. Codeforces 1023 B.Pair of Toys (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)

    B. Pair of Toys 智障题目(嘤嘤嘤~) 代码: 1 //B 2 #include<iostream> 3 #include<cstdio> 4 #include& ...

  4. codeforces 1023 D. Array Restoration 并查集

    D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration

    我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件 #include<bits/stdc++.h> #define x first #d ...

  6. E - Down or Right Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    http://codeforces.com/contest/1023/problem/E 交互题 #include <cstdio> #include <cstdlib> #i ...

  7. D. Recovering BST Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    http://codeforces.com/contest/1025/problem/D 树 dp 优化 f[x][y][0]=f[x][z][1] & f[z+1][y][0] ( gcd( ...

  8. Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) F 构造

    http://codeforces.com/contest/967/problem/F 题目大意: 有n个点,n*(n-1)/2条边的无向图,其中有m条路目前开启(即能走),剩下的都是关闭状态 定义: ...

  9. Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) E 贪心

    http://codeforces.com/contest/967/problem/E 题目大意: 给你一个数组a,a的长度为n 定义:b(i) = a(1)^a(2)^......^a(i), 问, ...

随机推荐

  1. JQuery排错关于$(document).ready(function(){});

    最近写了好多JQuery.也出了很多问题.不知道怎么回事.程序就不往下执行了.很是郁闷. 查了下资料,这里可能会有以下几种原因:1.js文件的引用路径不正确,特别是使用了命名空间,容易造成路径错误,使 ...

  2. SRM710 div1 MagicNim(博弈论)

    题目大意: 给出n+1堆石子,前n堆石子的数量是a[i],最后一堆只有1个石子,但是具有魔力 拿走该石子的一方可以选择接下来是进行普通的Nim游戏还是anti-nim游戏 问是先手必胜还是必败 首先拿 ...

  3. 【转】Win7装不上Office2010 提示MSXML 6.10.1129.0

    转自:http://zhidao.baidu.com/link?url=aZPbpBu0Fb7rc8HCb_NuonuZ4ET_BB8_NgZ96tCpB9dyuUyWVwMl78MLa7rh-rfx ...

  4. C# 获取ORACLE SYS.XMLTYPE "遇到不支持的 Oracle 数据类型 USERDEFINED"

    1.需要加函数 2.需要加表别名 select   a.XML.getclobval()  from TB1  a

  5. [洛谷P2568]GCD

    题目大意:给你$n(1\leqslant n\leqslant 10^7)$,求$\displaystyle\sum\limits_{x=1}^n\displaystyle\sum\limits_{y ...

  6. [POJ1631]Bridging signals

    题目大意:不知,根据样例猜测为最长上升子序列(竟然还对了) 题解:$O(n log_2 n)$,求二维偏序,(q为存答案的序列,a存原序列,len为答案) for(int i = 1; i <= ...

  7. 关于label标签的作用

    label标签的定义和用法: <label> 标签为 input 元素定义标注(标记). label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label ...

  8. CENSORING

    CENSORING 题目描述 FJ为它的奶牛订阅了很多杂志,balabala.......,其中有一些奶牛不宜的东西(比如如何煮牛排). FJ将杂志中所有的文章提取出来组成一个长度最多为10^5的字符 ...

  9. CSS3边框会动的信封

    <!DOCTYPE html><html> <head> <title>酷炫的CSS3</title> <meta charset=& ...

  10. 一些奇怪的JavaScript试题

    JavaScript有很多地方和我们熟知的C.Java等的编程习惯不同,这些不同会产生很多让人意想不到的事情.前段时间在知乎有人发了写Javascrtip试题,觉得挺好玩的,这里跟大家分享一下. 01 ...