[ZJOI2006]皇帝的烦恼
题解:
具有单调性的题目还是要多想想二分答案
不二分答案暴力dp是n^3的
非常不优秀
二分答案之后就比较好做
mx[i],nx[i]表示最多/最少几个与a[1]相同
代码:
#include <bits/stdc++.h> using namespace std; #define rint register int #define IL inline #define rep(i,h,t) for (rint i=h;i<=t;i++) #define dep(i,t,h) for (rint i=t;i>=h;i--) #define mid ((h+t)>>1) const int N=2e5; int n,m,a[N],mx[N],nx[N]; bool check(int x) { mx[1]=a[1],nx[1]=a[1]; rep(i,2,n) { mx[i]=min(a[1]-nx[i-1],a[i]); nx[i]=max(0,a[i]-((x-a[i-1])-(a[1]-mx[i-1]))); } if (nx[n]==0) return(1); else return(0); } int main() { freopen("1.in","r",stdin); freopen("1.out","w",stdout); ios::sync_with_stdio(false); int maxa=0; cin>>n; rep(i,1,n) cin>>a[i]; rep(i,1,n-1) maxa=max(maxa,a[i]+a[i+1]); int h=maxa,t=1e9; while (h<t) { if (check(mid)) t=mid; else h=mid+1; } cout<<h<<endl; return 0; }