我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件

#include<bits/stdc++.h>
#define x first
#define y second
#define ok cout << "ok" << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const long double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double Eps = 1e-;
const int N = 2e5+; int n, q, a[N], t, pre, pos, l[N];
bool vis[N]; const int MAXN = N;
int dp[MAXN][];
int mm[MAXN];
//初始化RMQ, b数组下标从1开始
void initRMQ(int n)
{
mm[] = -;
for(int i = ; i <= n;i++)//
{
mm[i] = ((i&(i-)) == )?mm[i-]+:mm[i-];//推出n在2的多少范围内
dp[i][] = a[i];
}
//RMQ操作求区间最小值
for(int j = ; j <= mm[n];j++)
for(int i = ;i + (<<j) - <= n;i++)
dp[i][j] = min(dp[i][j-],dp[i+(<<(j-))][j-]);
}
//在区间[x, y]查询最大值
int rmq(int x,int y)
{
int k = mm[y-x+];
return min(dp[x][k],dp[y-(<<k)+][k]);
}
int main(void) {
while(cin >> n >> q) {
t = , pos = ;
bool maflag = false;
for(int i = ; i <= n; i++) {
scanf("%d", a + i);
if(a[i] == q)
maflag = true;
if(a[i] == && pos == )
pos = i;
if(a[i] == )
a[i] = a[i-];
if(t == && a[i])//防止全是0
t = a[i];
}
// 都是0
if(t == ) {
printf("YES\n");
for(int i = ; i <= n; i++) {
printf("%d%c", q, i == n ? '\n' : ' ');
}
continue;
}
// 替代前缀0
for(int i = ; i <= n; i++) {
if(a[i] == )
a[i] = t;//相当于全是前导变成与第一个前导相同的
else
break;
}
// 没有出现最大值且有0
if(!maflag && pos) {
a[pos] = q;//最大值赋值给它
maflag = true;
}
if(!maflag) {
printf("NO\n");
continue;
}
// 判断是否重复出现
initRMQ(n);//rmq操作
pre = a[];
bool flag = true;
memset(vis, , sizeof vis);
for(int i = ; i <= n; vis[a[i]] = true, pre = a[i], l[a[i]] = i, i++) {
if(a[i] == pre) //a[i]用过并且令前导为a[i],a[i]的第一个编号是i
continue; //前面和后面一个相同
if(vis[a[i]] && rmq(l[a[i]] + , i - ) < a[i]) {
flag = false;//如果前面已经用过a[i] 用rmq查询这两个之间是最小值如果小于a[i]就不满足
break;
}
}
if(flag) {
printf("YES\n");
for(int i = ; i <= n; i++) {
printf("%d%c", a[i], i == n ? '\n' : ' ');
}
}
else
printf("NO\n");
} return ;
}

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration的更多相关文章

  1. 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 ...

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

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

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

    #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> ...

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

    从(1,1,n,n)每次只变一个坐标,进行询问. 如果问到对角线有距离限制, 再从(1,1,n/2,n/2)询问到(n/2,n/2,n,n) 记住前半部分贪心忘上走,后本部分贪心往右走 因为最后的路线 ...

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

    考场上只做出了ABDE C都挂了... 题解: A 题解: 模拟 判断前面一段是否相同,后面一段是否相同,长度是否够(不能有重叠) Code: #include<stdio.h> #inc ...

  6. 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( ...

  7. Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最长连续交替序列)

    B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...

  8. Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B. Weakened Common Divis

    题目链接 让你找一个数,使得这个数,可以被每个二元组的两个数中的一个数整除. 先将第一个二元组的两个数质因数分解一下,分解的质数加入set中,然后,对剩下的n-1个二元组进行遍历,每次遍历到的二元组对 ...

  9. Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    A : A. Doggo Recoloring time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. MySQL视图 索引 存储过程 触发器 函数

       视图: 也就是一个虚拟表(不是真实存在的),它的本质就是根据SQL语句获取动态的数据集,并为其命名.用户使用时只需要使用命名的视图即可获取结果集,并可以当做表来使用.它的作用就是方便查询操作,减 ...

  2. <20190102>收录些比较低级错误导致的主板故障现象

    今天收录俩个比较低级的错误. 故障现象:   水冷排风扇高速运转, 并无法调控. 现在CPU散热的水冷排都设计了三条线,   温控4Pin , 水泵线 3Pin  , 接在机箱上USB口取电的灯线或者 ...

  3. div放在li标签中,无法撑开li标签的问题

    作为一个前端菜鸟,我又碰到问题了,今天把div放到li标签中,发现div并没有把li标签撑开,而是在li标签边界之外,具体情况如下图所示: 那么,怎样才能达到预期的效果(每个li中放置一个div标签, ...

  4. css设置标签居中

    position: absolute;  //相对于已经定位的父元素的位置. left: 50%; top: 50%; transform: translate(-50%,50%);

  5. v-bind指令动态绑定class和内联样式style

    动态绑定class—概述 数据绑定(v-bind指令)一个常见需求是操作元素的 class 列表.因为class是元素的一个属性,我们可以用 v-bind 处理它们 我们只需要计算出表达式最终的字符串 ...

  6. read_csv报错Initializing from file failed

    Python版本:Python 3.6 pandas.read_csv() 报错 OSError: Initializing from file failed,一般由两种情况引起:一种是函数参数为路径 ...

  7. golang []byte和string相互转换

    测试例子 package main   import (     "fmt" )   func main() {     str2 := "hello"     ...

  8. UVA11853-Paintball(对偶图)

    Problem UVA11853-Paintball Accept:229  Submit:1830 Time Limit: 3000 mSec Problem Description You are ...

  9. Click to add to Favorites Troubleshooting: High Version Count Issues (Doc ID 296377.1)

    Copyright (c) 2018, Oracle. All rights reserved. Oracle Confidential. Click to add to Favorites Trou ...

  10. python3打包成exe---pyinstaller方法

    前言: 主要介绍python3的pyinstaller打包方法 pyinstaller安装参考地址:http://www.pyinstaller.org/ pywin32的下载地址:https://s ...