[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 $ios

題解:發現這個有可二分性,而在肯定字符集大小的狀況下,判斷是否合法較爲簡單。當$a_i>a_{i-1}$時,在後面補最小的字符;不然就去掉尾部的字符,而後作一個「加法」,考慮到位數較多,能夠用$\mathrm{map}$來記錄每個位置的字符。spa

卡點:在二分中把字符集爲$1$加入判斷,致使$\mathrm{TLE}$blog

 

C++ Code:ci

#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;
}
相關文章
相關標籤/搜索