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

  1. #include<bits/stdc++.h>
  2. #define x first
  3. #define y second
  4. #define ok cout << "ok" << endl;
  5. using namespace std;
  6. typedef long long ll;
  7. typedef unsigned long long ull;
  8. typedef vector<int> vi;
  9. typedef pair<int, int> pii;
  10. typedef pair<ll, ll> pll;
  11. const long double PI = acos(-1.0);
  12. const int INF = 0x3f3f3f3f;
  13. const ll LINF = 0x3f3f3f3f3f3f3f3f;
  14. const double Eps = 1e-;
  15. const int N = 2e5+;
  16.  
  17. int n, q, a[N], t, pre, pos, l[N];
  18. bool vis[N];
  19.  
  20. const int MAXN = N;
  21. int dp[MAXN][];
  22. int mm[MAXN];
  23. //初始化RMQ, b数组下标从1开始
  24. void initRMQ(int n)
  25. {
  26. mm[] = -;
  27. for(int i = ; i <= n;i++)//
  28. {
  29. mm[i] = ((i&(i-)) == )?mm[i-]+:mm[i-];//推出n在2的多少范围内
  30. dp[i][] = a[i];
  31. }
  32. //RMQ操作求区间最小值
  33. for(int j = ; j <= mm[n];j++)
  34. for(int i = ;i + (<<j) - <= n;i++)
  35. dp[i][j] = min(dp[i][j-],dp[i+(<<(j-))][j-]);
  36. }
  37. //在区间[x, y]查询最大值
  38. int rmq(int x,int y)
  39. {
  40. int k = mm[y-x+];
  41. return min(dp[x][k],dp[y-(<<k)+][k]);
  42. }
  43. int main(void) {
  44. while(cin >> n >> q) {
  45. t = , pos = ;
  46. bool maflag = false;
  47. for(int i = ; i <= n; i++) {
  48. scanf("%d", a + i);
  49. if(a[i] == q)
  50. maflag = true;
  51. if(a[i] == && pos == )
  52. pos = i;
  53. if(a[i] == )
  54. a[i] = a[i-];
  55. if(t == && a[i])//防止全是0
  56. t = a[i];
  57. }
  58. // 都是0
  59. if(t == ) {
  60. printf("YES\n");
  61. for(int i = ; i <= n; i++) {
  62. printf("%d%c", q, i == n ? '\n' : ' ');
  63. }
  64. continue;
  65. }
  66. // 替代前缀0
  67. for(int i = ; i <= n; i++) {
  68. if(a[i] == )
  69. a[i] = t;//相当于全是前导变成与第一个前导相同的
  70. else
  71. break;
  72. }
  73. // 没有出现最大值且有0
  74. if(!maflag && pos) {
  75. a[pos] = q;//最大值赋值给它
  76. maflag = true;
  77. }
  78. if(!maflag) {
  79. printf("NO\n");
  80. continue;
  81. }
  82. // 判断是否重复出现
  83. initRMQ(n);//rmq操作
  84. pre = a[];
  85. bool flag = true;
  86. memset(vis, , sizeof vis);
  87. for(int i = ; i <= n; vis[a[i]] = true, pre = a[i], l[a[i]] = i, i++) {
  88. if(a[i] == pre) //a[i]用过并且令前导为a[i],a[i]的第一个编号是i
  89. continue; //前面和后面一个相同
  90. if(vis[a[i]] && rmq(l[a[i]] + , i - ) < a[i]) {
  91. flag = false;//如果前面已经用过a[i] 用rmq查询这两个之间是最小值如果小于a[i]就不满足
  92. break;
  93. }
  94. }
  95. if(flag) {
  96. printf("YES\n");
  97. for(int i = ; i <= n; i++) {
  98. printf("%d%c", a[i], i == n ? '\n' : ' ');
  99. }
  100. }
  101. else
  102. printf("NO\n");
  103. }
  104.  
  105. return ;
  106. }

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. [SequenceFile_2] SequenceFile 的基本操作

    0. 说明 测试序列文件的读写操作 && 测试序列文件的排序操作 && 测试序列文件的合并操作 && 测试序列文件的压缩方式 && 测试 ...

  2. nginx 编译安装时的编译参数说明(不建议看)

    https://www.cnblogs.com/wazy/p/8108824.html ./configure --user=www \ #worker进程运行用户 --group=www \ #wo ...

  3. iOS解析XML实现省市区选择

    1.具体内容就不再赘述了.直接看关键代码. viewController.h // // ViewController.h // ParseXmlToRealizeChooseCityDemo // ...

  4. 【18】如何把数据存储到MongoDB数据库

    如何把数据存储到MongoDB数据库 时间:2018.10.31                   edit by :北鼻 一.mongoDB环境安装 需要使用mongoDB数据库的话需要安装环境, ...

  5. ABAP 内表访问表达式的性能

    内表访问表达式是ABAP 7.4中引入的重要特性,可以使语句变得更加简洁.美观.那么它的读写性能怎么样呢?我进行了一点点测试. 读取 测试代码,使用三种方式读取同一内表,分别是read table关键 ...

  6. ABAP 在被访问的程序中获取访问程序的全局变量

    前些日子接到过一个看起来比较普通的需求: 存在一个系统标准函数组FG01,内含函数模块FM00,FM01……等等.在系统程序中,FM00会调用FM01,通过FM01获取获取某些数据. 需求要求,复制一 ...

  7. golang的一些基础数据类型转换

    int -- string //string到int value_int,err:=strconv.Atoi(string) //int到string str:=strconv.Itoa(value_ ...

  8. ucml 连接虚字段

  9. go标准库的学习-crypto/des

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/des" des包实现了DES标准和TDEA算法,参见U.S. Fed ...

  10. 3902-luogu 最长不下降子区间

    题目 现有数列A1,A2,…An ,修改最少的数字,使得数列严格单调递增. 依旧是书上的题 但是书上的范围比较小 而 lg上的数据范围很大 按书上的 方法 是会超时 只能过一半的数据 但是 算法思路还 ...