[Agc029C]Lexicographic constraints_进制_二分答案_贪心
2024-09-21 17:14:03
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;
}
最新文章
- ognl
- poj[1187][Noi 01]陨石的秘密
- Android Studio 常用快捷键
- python mysql
- [Asp.Net]获取客户端ip和mac地址
- Centos6 安装vnc
- java类的加载顺序
- Entity Framework入门教程(14)---DbFirst下的存储过程
- Mysql的多种安装方法———rpm安装
- C#返回JSON格式数据
- python的subprocess模块执行shell命令
- Python中=、copy、deepcopy
- 深度学习中将类别标签映射到one_hot向量
- python 将字节字符串转换成十六进制字符串
- CSS 之 伪类及伪元素
- linux 的dmesg命令
- 【转】每天一个linux命令(6):rmdir 命令
- Now or later UVALive - 3211(2-SAT 最小值最大化)
- UVALive - 6872 Restaurant Ratings 数位dp
- phpword使用
热门文章
- java监测硬盘空间大小
- 安装YII
- ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
- access函数
- 【CTS2019】珍珠【生成函数,二项式反演】
- Java分布式互联网架构/微服务/高性能/springboot/springcloud2018年10月16日直播内容
- P3469 割点的应用
- 下载安装 binary editor
- Ubuntu 14.04 下安装redis后运行redis-cli 报出redis Connection refused错误【已解决】
- Raspberry Pi 4B FTP服务器配置