CodeForces 234F Fence :每列染成红或绿色,红绿使用有限制,使不同的颜色相邻面积最少 :dp

比较明显的dp了=

dp[i][j][flag]前i个栅栏中,红/绿颜料用了j的最小接触值

具体转移见程序:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 int dp[225][45005][2],a[225];
 6 int main()
 7 {
 8   int n,x,y,i,j,tmp=0,minx;
 9   freopen("input.txt","r",stdin);
10   freopen("output.txt","w",stdout);
11   scanf("%d%d%d",&n,&x,&y);
12   for (i=1;i<=n;i++) scanf("%d",&a[i]);
13   memset(dp,0x3f,sizeof(dp));
14   dp[0][0][0]=dp[0][0][1]=a[0]=0;
15   for (i=1;i<=n;i++){
16     tmp+=a[i];
17     for (j=a[i];j<=tmp;j++){
18       if (j<=x){
19           dp[i][j][0]=min(dp[i-1][j-a[i]][0],
20           dp[i-1][tmp-j][1]+min(a[i],a[i-1]));
21       }
22       if (j<=y){
23           dp[i][j][1]=min(dp[i-1][j-a[i]][1],
24           dp[i-1][tmp-j][0]+min(a[i],a[i-1]));
25       }
26     }
27   }
28   minx=0x3f3f3f3f;
29   for (i=0;i<=x;i++)
30     minx=min(minx,dp[n][i][0]);
31   for (i=0;i<=y;i++)
32     minx=min(minx,dp[n][i][1]);
33   if (minx==0x3f3f3f3f) printf("-1\n");
34   else printf("%d\n",minx);
35   return 0;
36 }
View Code

题目链接:http://codeforces.com/problemset/problem/234/F

posted on 2015-03-25 21:40  xiao_xin  阅读(227)  评论(0编辑  收藏  举报

导航