题意:给一个 n(2e5)长的数组,再给一个等差数列,问最多执行一次(添加等差数列)后的最大第 K 大是多少。Problem - D - Codeforces
分析:真的很惭愧,当时看到第 K 大,一眼数据结构,直接跑偏了,想了半天(明知不对就不能再硬想了)。。本来这样就不是正确的思考方式。对于最大值,考虑二分,对于第K大,只要处理后区间>=mid的个数多于K个即可,正常做肯定是 nm 的,想着优化肯定就是要优化掉 等差数列 那个复杂度,思考一下就能知道,每个位置能直接算出覆盖区间,所以在枚举的时候直接算出左右端点,最后跑一遍前缀和即可;和最近有个cf题Problem - C - Codeforces有点像,不过后者是很弱化版,但思维大方向是差不多的。
然后下标有点难算,细节比较多,个人马力还是太差了,要多练
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 15;
const int mo = 998244353;
#define pb push_back
#define pii pair<int,int>
#define ft first
#define sd second
#define ffor(i,a,b,c) for(int i=(a);i<(b);i+=(c))
#define For(i,a,b,c) for(int i=(a);i<=(b);i+=(c))
#define rfor(i,a,b,c) for(int i=(a);i>(b);i-=(c))
#define Rfor(i,a,b,c) for(int i=(a);i>=(b);i-=(c))
#define all(x) (x).begin(), (x).end()
#define debug1(x) cerr<<"! "<<x<<endl;
#define debug2(x,y) cerr<<"# "<<x<<" "<<y<<endl;
void slv() {
int n, K, m, c, d; cin >> n >> K >> m >> c >> d;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
auto check = [&] (ll k) {
vector<int> pr(n + 5);
for (int i = 1; i <= m; i++) {
if (a[i] >= k) {
pr[1]++;
pr[n + 1]--;
}
else {
int need;
if (d == 0) {
if (a[i] + c >= k) {
pr[m]++;
pr[min(n + 1, m + i)]--;
}
} else {
if (a[i] + c + 1ll * (i - 1) * d < k) continue;
pr[m]++;
need = max(0ll, (k - a[i] - c + d - 1) / d + 1);
int pos = min({n, m + i - 1, i + m - need});
pr[pos + 1]--;
}
}
}
for (int i = m + 1; i <= n; i++) {
if (a[i] >= k) {
pr[1]++;
pr[n + 1]--;
}
else {
int need;
if (d == 0) {
if (a[i] + c >= k) {
pr[i]++;
pr[min(n + 1, i + m)]--;
}
} else {
if (a[i] + c + 1ll * (m - 1) * d < k) continue;
pr[i]++;
need = max(0ll, (k - a[i] - c + d - 1) / d + 1);
int pos = min(n, i + m - need);
pr[pos + 1]--;
}
}
}
for (int i = 1; i <= n; i++) {
pr[i] += pr[i - 1];
if (pr[i] >= K)
return true;
}
return false;
};
ll l = 0, r = 1e18;
while (l < r) {
ll mid = l + r + 1 >> 1;
// debug1(mid)
if (check(mid))
l = mid;
else
r = mid - 1;
}
cout << l << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
slv();
}