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), 问, ...
随机推荐
- BZOJ4373 算术天才⑨与等差数列(线段树)
看上去很难维护,考虑找一些必要条件.首先显然最大值-最小值=k*(r-l).然后区间内的数需要模k同余.最后区间内的数两两不同(k=0除外).冷静一下可以发现这些条件组合起来就是充分的了. 考虑怎么维 ...
- Windows查看进程CMD命令和终止进程CMD命令
将小米路由器3刷机成openwrt的方法,请参考上篇文章< 家庭宽带多运营商接入方案>这里介绍怎么在已经刷成openwrt系统的小米路由器3上安装私有云nextcloud openwrt开 ...
- [Leetcode] remove element 删除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 创建dll
在制作dll的时候,如果全局变量不导出,而函数调用中,包含了全局变量,那么会出现全局变量没有值的问题. add.c #pragma once //强制无签名utf-8 #include "a ...
- Spring源码解析-配置文件的加载
spring是一个很有名的java开源框架,作为一名javaer还是有必要了解spring的设计原理和机制,beans.core.context作为spring的三个核心组件.而三个组件中最重要的就是 ...
- [bzoj 1208]STL水过
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 看网上的题解都用的手写数据结构……然而直接用set的lower_bound就水过去了 ...
- PHP正则匹配与替换的简单例子
PHP正则匹配与替换的简单例子,含一个匹配获取加租字体例子和一个匹配替换超链接的例子. 1.查找匹配 <b> 与 </b> 标签的内容: <?php $str = &qu ...
- is
MyType a = null; if (a is MyType) == False
- P值
https://baike.baidu.com/item/P%E5%80%BC/7083622?fr=aladdin https://baijiahao.baidu.com/s?id=15960976 ...