跳到正文

题解

题解:P14156 [ICPC 2022 Nanjing R] 聊天程序

Infinity_qwq约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年7月31日
收录
2026年7月31日
更新
2026年10月27日
篇幅
约 6 分钟
题目
P14156

版本

  1. v1当前

注意到问题问的是“求第 kk 大的最大值”,考虑二分答案。单调性显然:若 xx 合法,那么所有 ≤x\le x 的值合法。

考虑二分第 kk 大值 xx,使得有至少 kk 个数的值 ≥x\ge x。接下来考虑把每个 aia_i 拆开来看,会出现以下情况:

  • ai≥xa_i \ge x,则它本身就会对 kk 有贡献。
  • ai<xa_i < x,则需要考虑在等差数列的加成下如何满足第一个条件。

接下来思考如何推导单个 ii 对应的区间。显然,pp 首先要满足 p∈[max⁡(1,i−m+1),min⁡(i,n−m+1)]p \in [\max(1, i-m+1),\min(i, n-m+1)],记为基础区间 [L,R][L,R]。对于第二种情况,设我们需增量 needneed 为 x−aix-a_i,继续拆分成以下几种情况:

  • d=0d=0,说明只能增加固定量 cc,若 c≥needc \ge need,则所有覆盖 ii 的起点都可行,否则无贡献。
  • d>0d>0,仍然先考虑 c≥needc \ge need 的情况,此时只要覆盖 ii 就足够,否则设 q=⌈need−cd⌉q = \lceil \frac{need-c}{d} \rceil,若 q≥mq \ge m,即使 aia_i 加上数列中最大的数也无法产生贡献,否则在还满足 p≤i−qp \le i-q 的情况下产生贡献。
一个可能出现的问题

这个问题是作者在写这个题的时候产生的疑问。上述过程看似都是在已知 pp 的情况下计算,但实际上为了实现正确的时间复杂度,我们将计数转为区间覆盖,用差分数组统计,计算出哪些等差数列起点 pp 能使位置 ii 满足条件。最后再计算前缀和,计算贡献。

可能的坑点
  1. 十年 OI 一场空,不开 long long 见祖宗。
  2. 二分答案的范围最大应该是 101810^{18},不能设成 10910^9。
  3. 注意 d=0d=0 的判断。
  4. 二分答案本质多测,所以多测不清空,爆零两行泪。
AC code
C++
/*

time : 2026-07-31 15:51:10

contest :

Problem : 

Solution :

Code by 720872

*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<climits>
#include<bitset>
#define ll long long
#define int ll
#define ull unsigned long long
#define il inline
#define endl '\n'
#define y1 Y1
#define pii pair<int,int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define _1 first
#define _2 second
#define mkp make_pair
#define pb push_back
#define lowbit(x) x & (-x)
#define debug cout << "debug" << endl;
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
using namespace std;

inline ll read(){
	ll k = 0, f = 1, c = gc();
	for (; !isdigit(c); c = gc()) if (c == '-') f = -1;
	for (; isdigit(c); c = gc()) k = k * 10 + (c ^ 48);
	return k * f;
}
inline void put(ll x){
	if(x < 0) putchar('-'), x = -x;
	if(x < 10) putchar(x + '0');
	else put(x / 10), putchar(x % 10 + '0');
}

const int N = 2e5 + 5;
int n, k, m, c, d, a[N], f[N];
int ans, l, r = 1e18, cnt, sum, maxx;

bool check(int x){
	memset(f, 0, sizeof f);
	cnt = 0, sum = 0, maxx = 0;
	for(int i = 1; i <= n; i++){
		if(a[i] >= x) cnt++;
		else{
			int need = x - a[i] - c;
			if(need <= 0)
				f[max(1ll, i - m + 1)]++, f[min(i + 1, n - m + 2)]--;
			else if(d){
				int q = ceil(1.0 * need / d);
				if(q <= m && i - q > 0){
					int L = max(1ll, i - m + 1);
					int R = min(i - q, n - m + 1);
					if (L <= R)
						f[L]++, f[R + 1]--;
				}
			}
		}
	}
	for(int i = 1; i <= n - m + 1; i++)
		sum += f[i], maxx = max(maxx, sum);
	if(cnt + maxx >= k) return 1;
	else return 0;
}

signed main(){
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
//	ios::sync_with_stdio(false);
//	cin.tie(0); cout.tie(0);
	n = read(), k = read(), m = read(), c = read(), d = read();
	for(int i = 1; i <= n; i++)
		a[i] = read();
	while(l <= r){
		int mid = (l + r) >> 1;
		if(check(mid)) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	put(ans);
	return 0;
}
/*
input:
6 4 3 1 2
1 1 4 5 1 4
output:
4
*/
原文
存档评论1 条抓取于