Bzoj: [HNOI2005] Comb DP

Source: Internet
Author: User

: [HNOI2005] Comb time limit:10 Sec Memory limit:162 MB
submit:266 solved:125
[Submit] [Status] Description

Input

The first behavior of the integer L, wherein 4<=l<=100000, and 50% of the data to meet the l<=104, indicating the length of the lower side of the plank line segment. Second behavior L A positive integer a1,a2,..., AL, of which 1

Output

Contains only an integer d, which is the number of squares that need to be dug out of the board to make the comb area the largest.

Sample Input9
4 4 6 5 4 2 3 3 5Sample Output3
The general greedy strategy still needs a more rigorous proof. For example, this problem, position pos at the height of the possible should be h[i]+/-1 (pos-2<=i<=pos+2) A slight negligence, will be the scope of I wrong.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<map>using namespacestd;#defineMAXN 110000#defineINFL 0X3F3F3F3F3F3F3F3FLLtypedefLong LongQword;intH[MAXN];intpp[maxn][ +];; Qword dp[maxn][2][ +];inlinevoidDeal (Qword &X,qword y) {        if(x<y) x=y;}intMain () {Freopen ("Input.txt","R", stdin); inti,j,k,k2,x,y,z,n,m; Qword sum=0; scanf ("%d",&N);  for(i=1; i<=n;i++) {scanf ("%d", H +i); Sum+=H[i];  for(j=h[i]-1; j<=h[i]+1; j + +) {pp[i][++pp[i][0]]=J; if(I-1>=1) pp[i-1][++pp[i-1][0]]=J; if(i+1<=n) pp[i+1][++pp[i+1][0]]=J; if(I-2>=1) pp[i-2][++pp[i-2][0]]=J; if(i+2<=n) pp[i+2][++pp[i+2][0]]=J; }        }         for(i=1; i<=n;i++) {sort (Pp[i]+1, pp[i]+pp[i][0]+1); pp[i][0]=unique (&pp[i][1],&pp[i][pp[i][0]+1])-pp[i]-1;  for(j=pp[i][0]+1;j< A; j + +) pp[i][j]=0;  while(pp[i][0] && pp[i][pp[i][0]]>h[i]) pp[i][pp[i][0]--]=0; }         for(i=0; i<=n+1; i++)                 for(j=0;j<2; j + +)                         for(k=0;k< +; k++) Dp[i][j][k]=-INFL;  for(i=1; i<=pp[1][0];i++) dp[1][0][i]=dp[1][1][i]=pp[1][i];  for(i=2; i<=n;i++)        {                 for(j=1; j<=pp[i-1][0];j++)                {                         for(k=1; k<=pp[i][0];k++)                        {                                if(pp[i-1][j]<Pp[i][k]) {Deal (dp[i][0][k],dp[i-1][1][j]+Pp[i][k]); }Else if(pp[i-1][j]>Pp[i][k]) {Deal (dp[i][1][k],dp[i-1][0][j]+Pp[i][k]); }Else{deal (dp[i][0][k],dp[i-1][0][j]+Pp[i][k]); Deal (dp[i][1][k],dp[i-1][1][j]+Pp[i][k]); } }}} Qword ans=-INFL;  for(i=1; i<=pp[n][0];i++) {ans=max (ans,dp[n][0][i]); Ans=max (ans,dp[n][1][i]); } printf ("%lld", sum-ans);}

Bzoj: [HNOI2005] Comb DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.