【題解】Painting Fence
阿新 • • 發佈:2019-02-10
tin get line color template clas code temp endif
【題解】Painting Fence
分治模板。貪心加分治。直接\(O(n^2logn)\)分治過去。考慮一塊聯通的柱形是子問題的,是遞歸的,貪心分治就可。記得對\(r-l+1\)取\(min\)。
上好看的代碼
#include<bits/stdc++.h> #define RP(t,a,b) for(register int (t)=(a),edd_=(b);t<=edd_;++t) #define DRP(t,a,b) for(register int (t)=(a),edd_=(b);t>=edd_;--t) #define ERP(t,a) for(int t=head[a];t;t=e[t].nx) #define pushup(x) seg[(x)]=seg[(x)<<1]+seg[(x)<<1|1] #define midd register int mid=(l+r)>>1 #define TMP template<class ccf> #define rgt L,R,mid,r,pos<<1|1 #define lef L,R,l,mid,pos<<1 #define all 1,n,1 using namespace std;typedef long long ll; TMP inline ccf Max(ccf a,ccf b){return a<b?b:a;} TMP inline ccf Min(ccf a,ccf b){return a<b?a:b;} TMP inline ccf Abs(ccf a){return a<0?-a:a;} TMP inline ccf qr(ccf k){ char c=getchar();ccf x=0;int q=1; while(c<48||c>57)q=c==45?-1:q,c=getchar(); while(c>=48&&c<=57)x=x*10+c-48,c=getchar(); return q==-1?-x:x;} //-------------template&IO--------------------- const int maxn=5e3+15; const int inf=2e9+5; int data[maxn],n; int divd(int l,int r,int cut){ if(l>r) return 0; register int sav=inf,L=l,ret=0; RP(t,l,r) if(data[t]-cut>0&&sav>data[t]-cut) sav=data[t]-cut; RP(t,l,r+1) if(data[t]-cut-sav<=0||t>r) ret+=divd(L,t-1,cut+sav),L=t+1; return Min(sav+ret,r-l+1); } int main(){ #ifndef ONLINE_JUDGE freopen("color.in","r",stdin); freopen("color.out","w",stdout); #endif n=qr(1);RP(t,1,n)data[t]=qr(1); cout<<divd(1,n,0)<<endl; return 0; }
【題解】Painting Fence