Codeforces 1023 D.Array Restoration-RMQ(ST)区间查询最值 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)
这题想一下就会发现是只要两个相同的数之间没有比它小的就可以,就是保存一下数第一次出现和最后一次出现的位置,然后查询一下这个区间就可以,如果有0的话就进行填充。
这个题我是用RMQ(ST)进行查询的,在初始化的时候,如果有0就把0变成2e5+1,因为数据最大2e5,然后区间查询就可以了,关于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)的更多相关文章
- 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 ...
- 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& ...
- 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& ...
- 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 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration
我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件 #include<bits/stdc++.h> #define x first #d ...
- 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 ...
- 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( ...
- 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条路目前开启(即能走),剩下的都是关闭状态 定义: ...
- 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), 问, ...
随机推荐
- ActiveMQ+Zookeeper集群配置文档
Zookeeper + ActiveMQ 集群整合配置文档 一:使用ZooKeeper实现的MasterSlave实现方式 是对ActiveMQ进行高可用的一种有效的解决方案, 高可用的原理:使用Zo ...
- NET中解决KafKa多线程发送多主题
NET中解决KafKa多线程发送多主题 一般在KafKa消费程序中消费可以设置多个主题,那在同一程序中需要向KafKa发送不同主题的消息,如异常需要发到异常主题,正常的发送到正常的主题,这时候就需要实 ...
- [洛谷P1887]乘积最大3
题目大意:请你找出$m$个和为$n$的正整数,他们的乘积要尽可能的大.输出字典序最小的方案 题解:对于一些数,若它们的和相同,那么越接近它们的乘积越大. 卡点:无 C++ Code: #include ...
- 处理WebService asmx的经验
项目的需求,需要和一个.net系统进行数据交换,合作方提供了一个WebService接口.这个与一般的PHP POST或GET传值再查库拿数据的思路有点不一样,需要用到SOAP模块,处理方法也很简单, ...
- 使用babel把es6代码转成es5代码
第一步:创建一个web项目 使用命令:npm init 这个命令的目的是生成package.json. 执行第二步中的命令后生成的package.json的文件的内容是: { "name&q ...
- rsync 同步
1./usr/bin/rsync -vzrtopg --progress --include "weibo-service-server" --exclude "/*& ...
- SynchronizationContext.Post方法 代替
http://www.codeproject.com/KB/threads/SynchronizationContext.aspx看吧,不好,就将就的看下我的吧,呵呵!(没有直接翻译,不过大概的思路相 ...
- Spring学习--依赖注入的方式
Spring 依赖注入: 属性注入(最常使用) 构造函数注入 工厂方法注入(很少使用,不推荐) 属性注入:通过 setter 方法注入 Bean 的属性值或依赖的对象 , 使用<property ...
- Eclipse工具栏太多,自定义工具栏,去掉调试
Window --> Customize Perspective... --> Tool Bar Visibility 去掉勾选debug Tip:最新版本Customize Persp ...
- 【HDU4405】Aeroplane chess [期望DP]
Aeroplane chess Time Limit: 1 Sec Memory Limit: 32 MB[Submit][Stataus][Discuss] Description Hzz lov ...