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
#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的更多相关文章
- 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 ...
- 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> ...
- 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> ...
- 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) 记住前半部分贪心忘上走,后本部分贪心往右走 因为最后的路线 ...
- Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
考场上只做出了ABDE C都挂了... 题解: A 题解: 模拟 判断前面一段是否相同,后面一段是否相同,长度是否够(不能有重叠) Code: #include<stdio.h> #inc ...
- 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 #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 ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B. Weakened Common Divis
题目链接 让你找一个数,使得这个数,可以被每个二元组的两个数中的一个数整除. 先将第一个二元组的两个数质因数分解一下,分解的质数加入set中,然后,对剩下的n-1个二元组进行遍历,每次遍历到的二元组对 ...
- 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 ...
随机推荐
- 离群点检测与序列数据异常检测以及异常检测大杀器-iForest
1. 异常检测简介 异常检测,它的任务是发现与大部分其他对象不同的对象,我们称为异常对象.异常检测算法已经广泛应用于电信.互联网和信用卡的诈骗检测.贷款审批.电子商务.网络入侵和天气预报等领域.这些异 ...
- 【PAT】B1042 字符统计(20 分)
/* 15分的题很简单,但是自己写的时候在输入数据时没有考虑好下标 另外有忘记了输入字符时考虑是否有\n */ #include<stdio.h> #include<algorith ...
- SAP SD模块功能构成
- 建立标准编码规则(四)-C#编码规范分类及实现
1 现实中的问题 在任何一个公司,每个程序员编写的习惯可能都是不一样的,大到命名空间,小到变量的名字.甚至,我们去github,里面的大牛每个开源的项目的命名规则可能都不一样. 我们通常会遇到下面的问 ...
- 使用CefSharp跳转页面不弹出页面:
using CefSharp; using CefSharp.Wpf; namespace Common.Control { internal class CefSharpOpenPageSelf : ...
- [Eclipse+PyDev]ImportError: DLL load failed:找不到指定的模块 解决方案
1. 环境 Eclipse 4.4.2 Python 3.5 Window 8.1 2. 问题 在代码中import numpy时,提示 " ImportError: DLL load fa ...
- 【JavaScript】获取当前页的URL与window.location.href
原文:http://blog.csdn.net/yongh701/article/details/45688743 版权声明:本文为博主原创文章,未经博主允许欢迎乱转载,标好作者就可以了!感谢欣赏!觉 ...
- c++11の的左值、右值以及move,foward
左值和右值的定义 在C++中,可以放到赋值操作符=左边的是左值,可以放到赋值操作符右边的是右值.有些变量既可以当左值又可以当右值.进一步来讲,左值为Lvalue,其实L代表Location,表示在内存 ...
- 让你提前认识软件开发(31):数据库脚本中的begin与end
版权声明:本文为博主原创文章.对文章内容有不论什么意见或建议,欢迎与作者单独交流.作者QQ(微信):245924426. https://blog.csdn.net/zhouzxi/article/d ...
- 原生js删除元素
//删除id var idObject = document.getElementById('sidebar'); if (idObject != null) idObject.parentNode. ...