Codeforces Round #145 (Div. 2 F. Fence)简单三维DP

F. Fence

Vasya should paint a fence in front of his own cottage. The fence is a sequence of n wood

en boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number

the board fence using numbers 1, 2, ..., n from left to right. The height of the i-th board is hicentimeters.

Vasya has a 1 centimeter wide brush and the paint of two colors, red and green. Of course,

the amount of the paint is limited. Vasya counted the area he can paint each of the colors.

It turned out that he can not paint over a square centimeters of the fence red, and he can

not paint over b square centimeters green. Each board of the fence should be painted exactly

one of the two colors. Perhaps Vasya won't need one of the colors.

In addition, Vasya wants his fence to look smart. To do this, he should paint the fence so as

to minimize the value that Vasya called the fence unattractiveness value. Vasya believes that

two consecutive fence boards, painted different colors, look unattractive. The unattractiveness

 value of a fence is the total length of contact between the neighboring boards of various colors.

To make the fence look nice, you need to minimize the value as low as possible. Your task is

to find what is the minimum unattractiveness Vasya can get, if he paints his fence completely.

The picture shows the fence, where the heights of boards (from left to right) are 2,3,2,4,3,1.

The first and the fifth boards are painted red, the others are painted green. The first and the

second boards have contact length 2, the fourth and fifth boards have contact length 3,

the fifth and the sixth have contact length 1. Therefore, the unattractiveness of the given

painted fence is 2+3+1=6.

Input

The first line contains a single integer n (1 ≤ n ≤ 200) — the number of boards in Vasya's fence.

The second line contains two integers a and b (0 ≤ a, b ≤ 4·104) — the area that can be

painted red and the area that can be painted green, correspondingly.

The third line contains a sequence of n integers h1, h2, ..., hn (1 ≤ hi ≤ 200) — the heights

of the fence boards.

All numbers in the lines are separated by single spaces.

Output

Print a single number — the minimum unattractiveness value Vasya can get if he paints

his fence completely. If it is impossible to do, print  - 1.

Examples

4
5 7
3 3 4 1
output
3

input
3
2 3
1 3 1
output
2

input
3
3 3
2 2 2
output
-1

题意:

      N个栅栏,现在要给这些栅栏刷漆(每个栅栏只能刷一种颜色),

两种颜色可供选择,红色绿色(最多使用面积分别为a、b),如果两个

相邻栅栏颜色不同,将产生一个等于不同部分面积的权值,问怎么刷漆

能获得最小权值,输出最小权值。

思路:

      dp【i】【j】【0/1】表示前 i 个栅栏,红色使用 j 面积,当前

栅栏为红色/绿色所获得的最小代价,注意一下别忘了绿色部分的

限制。(开始还考虑要不要红色和绿色状态分别表示,实际上知道了

红色用了多少,绿色的使用面积也就确定了)。

代码实现:

#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int N=2e2+100;
const int M=4e4+100;
int arr[N],dp[N][M][2],sum[N];
int main() {
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  int n,a,b,ans;
  while(cin>>n) {
    cin>>a>>b;
    ans=INF;
    memset(sum,0,sizeof(sum));
    memset(dp,INF,sizeof(dp));
    for(int i=1; i<=n; i++) {
      cin>>arr[i];
      sum[i]=sum[i-1]+arr[i];
    }
    if(arr[1]<=a)dp[1][arr[1]][0]=0;//别忘了边界处理
    if(arr[1]<=b)dp[1][0][1]=0;
    for(int i=2; i<=n; i++) {
      for(int j=0; j<=a; j++) {
        if(j>=arr[i]&&(sum[i]-j<=b)) {
          dp[i][j][0]=min(dp[i-1][j-arr[i]][0],
                          dp[i-1][j-arr[i]][1]+min(arr[i],arr[i-1]));
        }
        if((sum[i]-j<=b)) {
          dp[i][j][1]=min(dp[i-1][j][1],
                          dp[i-1][j][0]+min(arr[i],arr[i-1]));
        }
      }
    }
    for(int j=0; j<=a; j++) {
      ans=min(ans,dp[n][j][0]);
      ans=min(ans,dp[n][j][1]);
    }
    if(ans>=INF) {
      cout<<-1<<endl;
    } else {
      cout<<ans<<endl;
    }
  }
  return 0;
}

THE END;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值