https://codeforces.com/contest/1400/problem/E
可以横着把有连续的数字-1,或者把某一个数字清0,问你最少操作几次,分治
555 555 555操作3次,1 1 1操作1次
具体看代码把
#include<iostream> #include<cstring> #include<queue> #include<algorithm> #include<vector> using namespace std; typedef long long ll; const int maxn = 5050+1; ll list[maxn]; int n; int cal(int l,int r){ //cout<<l<<" "<<r<<endl; if(l <=-1 || r <= -1) return 0; if(l >= r){ return min(1LL*r - 1LL*l + 1,list[l]); } ll cnt = 1e15; for(int i = l;i<=r;i++){ cnt = min(list[i],cnt); } int p = -1; for(int i = l;i<=r;i++){ list[i] -= cnt; if(list[i] == 0 && p == -1) p = i; } return min(cnt + cal(l,p-1) + cal(p+1,r),1LL*r - 1LL*l + 1); } int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>list[i]; } ll ans = cal(1,n); // cout<<endl; cout<<ans<<endl; return 0; }