好题。
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;
}