codeforces #145 B Fence

B. Fence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
input.txt
output
output.txt

Vasya should paint a fence in front of his own cottage. The fence is a sequence of n wooden 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 hi centimeters.

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. Theunattractiveness 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.

Sample test(s)
input
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
/**
    题目大意:某人要用red和green两种染料刷墙,问怎么样才能让两种颜色的连接数量最少?
    给定red颜色的数量a和green颜色的数量b,还有每面墙的高度。
    要求是,每一面墙只能用一种颜色刷,并且要刷满,不能只刷一部分。如果没有能够满足条件的刷法。输出-1

    思路:动态规划
    每一面墙可以用两种颜色刷,但是每种颜色有数量限制,并且还要求两种颜色连接数最少,用动态规划解决最优化。
    dp[i][j][s]表示前i面墙刷了j面red颜色并且最后一面为s颜色时,两种颜色的连接数最少数量,s为0或1,表示red和green。

    初始化dp为无穷大,但显然应该有dp[0][0][0]=0,dp[0][0][1]。

    当我们刷完第i面墙时,我们需要枚举到目前为止0颜色的数量(也就是枚举1颜色,除了0就是1),并考虑第i+1面应该刷什么,所以:
    状态转移:
    1,       首先讨论颜色0,
             如果下一面刷    颜色0,若当前刷的0总数量不大于总数a(j+num[i+1]),则
             dp[i+1][j+num[i+1]][0]=dp[i][j][0],否则,就不能把第i+1面墙刷成颜色0。
             如果下一面刷颜色1,若当前刷的1总数量不大于总数b(sum[i+1]-j),则
             dp[i+1][j][1]=dp[i][j][0]+min(num[i],num[i+1]),否则,就不能把第i+1面墙变成1。
    2,      接着讨论颜色1,
             如果下一面刷    颜色0,若当前刷的0总数量不大于总数b(j+num[i+1]),则
             dp[i+1][j+num[i+1]][0]=dp[i][j][1]+min(num[i],num[i+1]),否则,就不能把第i+1面墙刷成颜色0。
             如果下一面刷颜色1,若当前刷的1总数量不大于总数b(sum[i+1]-j),则
             dp[i+1][j][1]=dp[i][j][1],否则,就不能把第i+1面墙变成1。
    最终,枚举枚举dp[n][j][s],选择第n面墙中最小的那个dp,就是答案。
    但是,如果最小值等于初始化的值,那说明没有可以满足条件的刷墙的方法,需要输出-1.

**/
#include<iostream>
#include<cstdio>

using namespace std;

const int N = 202;
const int inf = ~0u>>2;//Max
int dp[N][N*N][2],num[N],sum[N];//dp,num[i]每一面的高度,sum[i]到第i面时的总高度
inline void chkmin(int &a, const int &b)//简化赋值
{
    if(a>b)a=b;
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,a,b;
    /**输入**/
    cin>>n>>a>>b;
    for(int i=1; i<=n; i++)
        cin>>num[i];
        
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+num[i];
    /**初始化**/
    for(int i = 0; i <= n; i++)
        for(int j = 0; j <=a; j++)
            for(int p = 0; p < 2; p++)
                dp[i][j][p] = inf;
    dp[0][0][0] = 0;
    dp[0][0][1] = 0;
    /**状态转移**/
    for(int i = 0; i < n; i++)
        for(int j = 0; j <= a; j++)
        {
            if(dp[i][j][0] != inf)
            {
                int v = dp[i][j][0];
                if(j + num[i+1] <= a)//第i+1面墙刷颜色0
                    chkmin(dp[i+1][j+num[i+1]][0],v);
                if(sum[i+1] - j <= b)//第i+1面墙刷颜色1
                    chkmin(dp[i+1][j][1],v+min(num[i],num[i+1]));
            }
            if(dp[i][j][1] != inf)
            {
                int v = dp[i][j][1];
                if(j + num[i+1] <= a)//第i+1面强刷颜色0
                    chkmin(dp[i+1][j+num[i+1]][0],v+min(num[i],num[i+1]));
                if(sum[i+1] - j <= b)//第i+1面墙刷颜色1
                    chkmin(dp[i+1][j][1],v);
            }
        }
    int ans = inf;
    for(int i = 0; i <=a ; i++)
        for(int j = 0; j < 2; j++)
            chkmin(ans,dp[n][i][j]);//寻找最大值,即答案
    if(ans == inf)
        cout<< -1 << endl;
    else
        cout<< ans << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值