Fence(CF-324F)
Problem Description
Vasya should paint a fence in front of his own cottage. The fence is a sequence of?nwooden 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?unattractivenessvalue. 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
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
題意:現(xiàn)在有 n 個(gè)柵欄要刷漆,每個(gè)柵欄只能刷紅色、綠色的一種顏色,最多使用顏色的面積為 x、y,現(xiàn)在給出 n 個(gè)柵欄的大小,如果相鄰柵欄顏色不同,那么會產(chǎn)生一個(gè)等于不同部分面積的權(quán)值,求最小權(quán)值,如果無法獲得最小權(quán)值,輸出 -1
思路:三維 DP
設(shè) dp[i][j][k] 代表對前?i 個(gè)柵欄來說,使用顏色為 k 時(shí),這 i 個(gè)柵欄使用顏色為紅色面積為 j 時(shí)的最小代價(jià)
由于只能染色紅色或綠色,那么 k 的取值為 0、1,于是有:
- dp[i][j][0]=min(dp[i-1][j-a[i]][0],dp[i-1][j-a[i]][1]+min(a[i],a[i-1]) )
- dp[i][j][1]=min(dp[i-1][j][1],dp[i-1][j][0]+min(a[i],a[i-1]) )
需要注意的是,由于第二維是以紅色面積為基準(zhǔn)的,但不要忘記綠色的限制值,由于柵欄的面積是固定的,因此可利用前綴和來統(tǒng)計(jì)柵欄的面積,這樣一來,只要知道了紅色的面積,那么綠色的面積也就知道了
此外,注意本題要使用文件讀寫
Source Program
#include<bits/stdc++.h> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 100000+5; const int dx[] = {-1,1,0,0,-1,-1,1,1}; const int dy[] = {0,0,-1,1,-1,1,-1,1}; using namespace std;int a[N]; int sum[N]; int dp[200+5][40000+5][2]; int main(){freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);int n;int x,y;scanf("%d",&n);scanf("%d%d",&x,&y);for(int i=1;i<=n;i++){scanf("%d",&a[i]);sum[i]=sum[i-1]+a[i];}memset(dp,INF,sizeof(dp));if(a[1]<=x)dp[1][a[1]][0]=0;if(a[1]<=y)dp[1][0][1]=0;for(int i=2;i<=n;i++){//i個(gè)柵欄for(int j=0;j<=x;j++){//使用紅色顏色面積int sub=min(a[i],a[i-1]);//相鄰柵欄的代價(jià)if(sum[i]-j<=y){//使用綠色的面積不超過y時(shí)dp[i][j][1]=min(dp[i-1][j][1],dp[i-1][j][0]+sub);if(a[i]<=j)//使用紅色面積不超過j時(shí)dp[i][j][0]=min(dp[i-1][j-a[i]][0],dp[i-1][j-a[i]][1]+sub);}}}int minn=INF;for(int i=0;i<=x;i++){minn=min(minn,dp[n][i][0]);minn=min(minn,dp[n][i][1]);}if(minn==INF)printf("-1\n");elseprintf("%d\n",minn);return 0; }?
總結(jié)
以上是生活随笔為你收集整理的Fence(CF-324F)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图论 —— 图的连通性 —— Tarja
- 下一篇: 家庭作业(信息学奥赛一本通-T1430)