现在的位置: 首页 > 综合 > 正文

Codeforces 234 F. Fence

2017年11月24日 ⁄ 综合 ⁄ 共 3181字 ⁄ 字号 评论关闭

DP

dp[i][j][k]前i个栏杆中,有j个被涂成red,当前的栏杆颜色 0 red 1 green

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF=(1<<30);

int n,a,b,sum;
int h[222],dp[2][40040][2];

int main()
{
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	scanf("%d%d%d",&n,&a,&b);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",h+i);
		sum+=h[i];
	}
	memset(dp[0],-1,sizeof(dp[0]));
	dp[0][0][1]=dp[0][0][0]=0;
	int now=1,pre=0;
	for(int i=1;i<=n;i++,swap(now,pre))
	{
		memset(dp[now],-1,sizeof(dp[now]));
		int mi=min(h[i],h[i-1]);
		for(int j=0;j<=a;j++)
		{
			int c=j+h[i];
			if(dp[pre][j][0]!=-1)
			{
				if(dp[now][c][0]==-1||dp[pre][j][0]<dp[now][c][0])
					dp[now][c][0]=dp[pre][j][0];
				if(dp[now][j][1]==-1||dp[pre][j][0]+mi<dp[now][j][1])
					dp[now][j][1]=dp[pre][j][0]+mi;
			}
			if(dp[pre][j][1]!=-1)
			{
				if(dp[now][c][0]==-1||dp[pre][j][1]+mi<dp[now][c][0])
					dp[now][c][0]=dp[pre][j][1]+mi;
				if(dp[now][j][1]==-1||dp[pre][j][1]<dp[now][j][1])
					dp[now][j][1]=dp[pre][j][1];
			}
		}
	}

	int ans=INF;
	now=pre;
	for(int i=0;i<=a;i++)
	{
		if(sum-i<=b)
		{
			if(dp[now][i][0]!=-1)
				ans=min(ans,dp[now][i][0]);
			if(dp[now][i][1]!=-1)
				ans=min(ans,dp[now][i][1]);
		}
	}

	if(ans==INF) ans=-1;

	printf("%d\n",ans);

	return 0;
}

抱歉!评论已关闭.