Codeforces Round #145 (Div. 2, ACM-ICPC Rules) Fence

來源:互聯網
上載者:User
#include<stdio.h>#include<string.h>#define eps 0x3f//這個值不可以初始化ans,實在太小int dp[203][40002][2],h[203],sh[203];//不懂了,看了大神代碼,寫下自己的心得吧//已經過每一塊板時,紅色用的數量為分割狀態//以後記住這種關聯狀態只有確定一種量的值就好//轉移狀態是看前一個板子的顏色。//x紅y藍 0紅,1藍
//其實就是個背包,這時候要明確顏料是可以省下的。
//抽象的背包只是代價計算不同而已。
//記得eps的作用,把不合理的情況都變得無限大int min(int a,int b){    return a<b?a:b;}int main(){    int n,x,y,ans,i,j;    freopen("input.txt","r",stdin);//這個格式記下來吧freopen("output.txt","w",stdout);    h[0]=sh[0]=0;ans=0x3f3f3f3f;    scanf("%d%d%d",&n,&x,&y);    memset(dp,eps,sizeof(dp));    dp[0][0][1]=dp[0][0][0]=0;    for(i=1;i<=n;i++)    {        scanf("%d",&h[i]);        sh[i]=sh[i-1]+h[i];    }    for(i=1;i<=n;i++)      for(j=min(sh[i],x);j>=0;j--)        if(sh[i]-j<=y)       {          if(sh[i]-j>=h[i])dp[i][j][1]=min(dp[i-1][j][1],dp[i-1][j][0]+min(h[i-1],h[i]));//狀態轉移過程          if(j>=h[i])dp[i][j][0]=min(dp[i-1][j-h[i]][0],dp[i-1][j-h[i]][1]+min(h[i-1],h[i]));          if(i==n)ans=min(ans,min(dp[i][j][0],dp[i][j][1]));       }         else break;      if(ans>=0x3f3f3f3f)printf("-1\n");      else printf("%d\n",ans);    fclose(stdin);fclose(stdout);      return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.