我们知道不满足的肯定是两边大中间小的,这样就用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. Linux 小知识翻译 - 「协议(protocol)」

    对于理解服务器和网络来说,「协议」是不可缺少的概念. 「协议(protocol)」有「规则,规定」的意思. 实际上「协议」的函数很广,在通信领域,「协议」规定了「在通信时,什么样的情况下,以什么样的顺 ...

  2. Vue仿抽屉

    创建VUE项目的步骤: npm install vue-cli -g vue init webpack myproject cd myproject npm run dev 组件:它是可扩展的html ...

  3. django知识点回顾(上)

    Django---知识点: 1. 配置文件: media: avatar = models.FileField(upload_to='avatar')#数据库里的model MEDIA_ROOT=os ...

  4. 如何在sublime编辑器中,执行命令行脚本

    我有个愿意,在执行命令行时,不打开那个黑乎乎命令行窗口,如果编辑器内置支持就好了. 打开vs code 和 sublime,分别按快捷键 Ctrl + ·(tab键上面那个键),vs code可以提供 ...

  5. trace文件解读

    *********************************************************************示例:全表扫描的10046文件解读************** ...

  6. ubuntu 16.04 SS安装及配置

    安装SS客户端 安装pip3 一般情况下,pip3安装的版本比pip安装的新,pip安装的版本比apt安装的新,这里选择最新版本. sudo apt install python3-pip 安装SS ...

  7. 迭代器协议和for循环工作机制

    一.递归和迭代 举个例子 递归:假如我去问路,路人甲看我长得盛世容颜,但是他不知道,他就去帮我问路人乙去了,路人乙跟路人甲说我也不知道,但一看路人甲美若天仙,就说,我去帮你问问路人丙,...完了可能得 ...

  8. OpenGL ES SL 3.0规范中以前的attribute改成了in varying改成了out

           OpenGL ES和OpenGL的图标 关于“OpenGL ES SL 3.0规范中以前的attribute改成了in varying改成了out”这个问题,做一阐述: 1.关键字的小修 ...

  9. matlab fspecial

    Matlab 的fspecial函数用法 fspecial函数用于建立预定义的滤波算子,其语法格式为:h = fspecial(type)h = fspecial(type,para)其中type指定 ...

  10. Html5 手机端网页

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...