【题解】Painting Fence

【题解】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;
}
 
上一篇:Scene视图辅助线绘制


下一篇:澳洲篇:墨尔本工业厂商优化3个月,Google地图排第一,网站关键词进首页!