[Atcoder AGC029C]Lexicographic constraints
题目大意:给定$n$个字符串的长度$a_i$,问至少用几种字符可以构造出字符串$s_1\sim s_n$,满足$|s_i|=a_i$且$s_1<s_2<\cdots<s_n$。 $ n\leqslant 2\times10^5,1\leqslant a_i\leqslant10^9 $
题解:发现这个有可二分性,而在确定字符集大小的情况下,判断是否合法较为简单。当$a_i>a_{i-1}$时,在后面补最小的字符;否则就去掉尾部的字符,然后做一个“加法”,考虑到位数较多,可以用$\mathrm{map}$来记录每一个位置的字符。
卡点:在二分中把字符集为$1$加入判断,导致$\mathrm{TLE}$
C++ Code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
const int maxn = 2e5 + 10; int n, a[maxn], p, flag = 1;
bool check(int k) {
std::map<int, int> M; M.clear();
for (int i = 1; i <= n; ++i) if (a[i] <= a[i - 1]) {
while (!M.empty() && M.rbegin() -> first > a[i]) M.erase(--M.end());
for (p = a[i]; p && ++M[p] == k; --p) M[p] = 0;
if (!p) return false;
}
return true;
} int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> n;
for (int i = 1; i <= n; ++i) std::cin >> a[i], flag &= a[i] > a[i - 1];
if (flag) return std::cout << "1\n", 0;
int l = 2, r = n, ans = n;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) r = mid - 1, ans = mid;
else l = mid + 1;
}
std::cout << ans << '\n';
return 0;
}
[Atcoder AGC029C]Lexicographic constraints的更多相关文章
- AGC029C - Lexicographic constraints
记录我心路历程吧,这道小水题暴露出我很多问题. 给定 \(n\) 个字符串长度 \(a_i\) ,求字符集最小多大,才能构造出按字典序比较 \(s_1 < s_2 < \dots < ...
- 「AGC029C」Lexicographic constraints
「AGC029C」Lexicographic constraints 传送门 好像这个题非常 easy. 首先这个答案显然具有可二分性,所以问题转化为如何判定给定的 \(k\) 是否可行. 如果 \( ...
- [Agc029C]Lexicographic constraints_进制_二分答案_贪心
Lexicographic constraints 题目链接:https://atcoder.jp/contests/agc029/tasks/agc029_c 数据范围:略. 题解: 二分是显然的, ...
- @atcoder - AGC036F@ Square Constraints
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个整数 N,统计有多少个 0~2N-1 的排列 \(P_0 ...
- AT4502-[AGC029C]Lexicographic constraints【二分,栈】
正题 题目链接:https://www.luogu.com.cn/problem/AT4502 题目大意 给出\(n\)个长度\(S\),求一个最小\(m\)表示用大小为\(m\)的字符集构造出\(n ...
- 【AtCoder】AGC029(A-E)
A - Irreversible operation 题解 把每个B后面的W个数累加起来即可 代码 #include <bits/stdc++.h> #define fi first #d ...
- AtCoder Beginner Contest 082 B - Two Anagrams
题目链接:https://abc082.contest.atcoder.jp/tasks/abc082_b Time limit : 2sec / Memory limit : 256MB Score ...
- AtCoder Regular Contest 069 D
D - Menagerie Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Snuke, w ...
- AtCoder Beginner Contest 069【A,水,B,水,C,数学,D,暴力】
A - K-City Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement In K-city, ...
随机推荐
- vue-router 在新窗口打开页面的功能
项目中,需要点击链接后再新窗口打开页面,大家知道vue是单页面应用开发框架,那么也不是不可以实现这个功能 很简单,详情看下面 1.<router-link>标签实现新窗口打开 <ro ...
- find、locate、whereis、which和type
1. find $ find . -name '*' 2. locate 很快速的搜寻档案系统内是否有指定的档案,比find要快很多 其方法是先建立一个包括系统内所有档案名称及路径的资料库,之后当寻找 ...
- 微信小程序开发--常用开发实例
一.常用商品列表的换行排布 <view class="box_max"> <view class="box_min">限时秒杀</ ...
- android 实时获取网速
public class NetSpeed { private static final String TAG = NetSpeed.class.getSimpleName(); private lo ...
- logger(二)logback简介及其实现原理
一.logback简介 logback是log4j创始人写的,性能比log4j要好,目前主要分为3个模块 logback-core:核心代码模块 logback-classic:log4j的一个改良版 ...
- 【学习笔记】大数据技术原理与应用(MOOC视频、厦门大学林子雨)
1 大数据概述 大数据特性:4v volume velocity variety value 即大量化.快速化.多样化.价值密度低 数据量大:大数据摩尔定律 快速化:从数据的生成到消耗,时间窗口小,可 ...
- sqlmap基本信息及参数使用方法
当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数 2.判断可以用那种SQL注入技术来注入 3.识别出哪种数据库 4.根据用户选择,读取哪些数据 sqlmap支持五种不同的注入模式: ...
- Linux shell if条件判断2
前面介绍linux shell的if判断的语法,现在再补充一点. Linux shell if条件判断1 分支判断结构 if , case 下面两个结构语法,已经在前面有过示例. 结构1: ...
- linux系统最大TCP连接数限制
2017-12-28 17:48:21 chenlin465373800 阅读数 16189 不太对 本博客为转载,原文请参见<a href="http://blog.51cto ...
- 在eclipse中安装使用lombok插件
Eclipse安装lombok插件 1.下载lombok.jar,lombok.jar官方下载地址:https://projectlombok.org/download 2.双击下载好的lombak. ...