题解:AT_agc040_e [AGC040E] Prefix Suffix Addition
OldDriverTree · · 题解
好题。
Solution
首先如果只有第一种操作 / 第二种操作,那么答案就为
然后可以发现本题就相当于将
但是这样复杂度直接升天,继续挖掘性质。
为了方便观察,我们先移项得到
所以我们只需要记录一个
现在的转移方程就为(为了方便,这里令
然后考虑如何动态维护
时间复杂度
Code
//when you use vector or deque,pay attention to the size of it.
//by OldDirverTree
#include<bits/stdc++.h>
//#include<atcoder/all>
#define P pair<int,int>
#define int long long
#define mid (l+r>>1)
using namespace std;
//using namespace atcoder;
//using mint=modint998244353;
int n,ans,p,q;
struct custom_hash
{
static uint64_t splitmix64(uint64_t x) {
x+=0x9e3779b97f4a7c15;
x=(x^(x>>30) )*0xbf58476d1ce4e5b9;
x=(x^(x>>27) )*0x94d049bb133111eb;
return x^(x>>31);
}
size_t operator() (uint64_t x) const {
static const uint64_t FIXED_RANDOM=chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x+FIXED_RANDOM);
}
};
int read() {
int x=0; bool _=true; char c=0;
while (!isdigit(c) ) _&=(c!='-'),c=getchar();
while (isdigit(c) ) x=x*10+(c&15),c=getchar();
return _?x:-x;
}
main()
{
n=read();
for (int i=1,lst=0;i<=n+1;i++) {
int val=i<=n?read():0;
if (val<q) ans++,q=max(min(p,val-lst+q),0ll),p=0;
else if (val<lst) p=max(min(p,val-lst+q),0ll);
else p=min(p+val-lst,q),q+=val-lst; lst=val;
}
printf("%lld",ans);
return 0;
}