题解:P14156 [ICPC 2022 Nanjing R] 聊天程序
Infinity_qwq · · 题解
注意到问题问的是“求第
考虑二分第
接下来思考如何推导单个
::::info[一个可能出现的问题]
这个问题是作者在写这个题的时候产生的疑问。上述过程看似都是在已知
::::warning[可能的坑点]{open}
- 十年 OI 一场空,不开
long long见祖宗。 - 二分答案的范围最大应该是
10^{18} ,不能设成10^9 。 - 注意
d=0 的判断。 -
二分答案本质多测,所以多测不清空,爆零两行泪。
::::success[AC code]
/*
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
*/
::::