Lexicographic constraints

题目链接https://atcoder.jp/contests/agc029/tasks/agc029_c

数据范围:略。


题解

二分是显然的,因为题目具有单调性。

但是怎么验证呢?

显然是贪心地验证,就是要$S_i$是满足条件最小的。

我的办法是维护一棵线段树,因为如果$A_i > A_{i - 1}$的话,只需要在后面加上极小字符。

然后只需要开一棵动态开点的权值线段树,维护这段区间是不是全是极大字符。如果是的话就不可以再变大了。

否则的话就可以,修改的话区间修改单点修改。

但其实不用这么麻烦,我们完全可以用一个$ map $胜任。

即,我们维护出来相当于一个$ mid $进制的东西。$ map $里存的是每一段的结尾,是什么。前面有一段空挡。

$ map $是有序的,随便搞一搞就好。

代码

#include <bits/stdc++.h>

#define N 200010 

using namespace std;

typedef long long ll;

char *p1, *p2, buf[100000];

#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )

int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
} int a[N], n; map <ll, ll> MP; bool check(int x) {
MP.clear();
for (int i = 2; i <= n; i ++ ) {
if (a[i - 1] >= a[i]) {
if (x == 1) {
return false;
}
while (!MP.empty()) {
ll mx = MP.rbegin() -> first;
if (mx > a[i]) {
MP.erase(mx);
}
else {
break;
}
} int j = a[i];
while (j > 0 && MP[j] + 1 == x) {
MP.erase(j);
j -- ;
}
if (!j) {
return false;
}
MP[j] ++ ;
}
}
return true;
} int main() {
n = rd();
for (int i = 1; i <= n; i ++ ) {
a[i] = rd();
} int l = 1, r = n, ans = n;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
cout << ans << endl ;
return 0;
}

最新文章

  1. ognl
  2. poj[1187][Noi 01]陨石的秘密
  3. Android Studio 常用快捷键
  4. python mysql
  5. [Asp.Net]获取客户端ip和mac地址
  6. Centos6 安装vnc
  7. java类的加载顺序
  8. Entity Framework入门教程(14)---DbFirst下的存储过程
  9. Mysql的多种安装方法———rpm安装
  10. C#返回JSON格式数据
  11. python的subprocess模块执行shell命令
  12. Python中=、copy、deepcopy
  13. 深度学习中将类别标签映射到one_hot向量
  14. python 将字节字符串转换成十六进制字符串
  15. CSS 之 伪类及伪元素
  16. linux 的dmesg命令
  17. 【转】每天一个linux命令(6):rmdir 命令
  18. Now or later UVALive - 3211(2-SAT 最小值最大化)
  19. UVALive - 6872 Restaurant Ratings 数位dp
  20. phpword使用

热门文章

  1. java监测硬盘空间大小
  2. 安装YII
  3. ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
  4. access函数
  5. 【CTS2019】珍珠【生成函数,二项式反演】
  6. Java分布式互联网架构/微服务/高性能/springboot/springcloud2018年10月16日直播内容
  7. P3469 割点的应用
  8. 下载安装 binary editor
  9. Ubuntu 14.04 下安装redis后运行redis-cli 报出redis Connection refused错误【已解决】
  10. Raspberry Pi 4B FTP服务器配置