题解:CF2244G Yura and Deadlines
zhangzirui66 · · 题解
一眼 dp,那先看这个复杂的转移条件,设我们试图从
如果
如果
但要是我们把如果删掉呢?会发现在
因此转移条件就变成了
先看条件
再看条件
按照上面 dp 即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
bool Begin;
const int inf = 0x3f3f3f3f3f3f3f3f, MAX_Memory = 256;
int n, a[1000005], b[1000005];
vector<pair<int, int> > lst[1000005];
int lowbit(int x){
return (x & -x);
}
void update(int x, int val){
for(int i = x; i <= n; i += lowbit(i)){
b[i] = max(b[i], val);
}
}
int query(int x){
if(x < 0) return 0;
int res = 0;
for(int i = x; i; i -= lowbit(i)){
res = max(res, b[i]);
}
return res;
}
void Solve(int Case){
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i];
b[i] = 0; lst[i].clear(); // lst[i] 记录在 i 时刻才能满足第二个转移条件
}
int ans = 0;
for(int i = 1; i <= n; i ++){
for(int j = 0; j < lst[i].size(); j ++){
update(lst[i][j].first, lst[i][j].second);
}
ans = max(ans, a[i] + query(i - a[i] - 1));
if(i + a[i] + 1 <= n){
lst[i + a[i] + 1].push_back({i, a[i] + query(i - a[i] - 1)});
}
}
cout << ans << "\n";
}
bool End;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
cin >> t;
for(int i = 1; i <= t; i ++){
Solve(i);
}
cerr << "\nMemory Limit: " << abs(&End - &Begin) / 1048576 << "MB / " << MAX_Memory << "MB";
if(abs(&End - &Begin) > MAX_Memory * 1048576) cerr << "\nMemory is Not Enough!"; return 0;
}