C. Hills
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output
Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction.
From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.
The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?
However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here denotes n divided by two, rounded up.
Input
The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence.
Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence.
Output
Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses.
Examples
input
Copy
5 1 1 1 1 1
output
Copy
1 2 2
input
Copy
3 1 2 3
output
Copy
0 2
input
Copy
5 1 2 3 2 2
output
Copy
0 1 3
Note
In the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.
In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction.
题意:有n座山,你要在山上建房子,建房子的山需要满足的条件是两边山的高度要严格小于它,你可以降低一些山的高度,每减少一个单位的高度,需要消耗一个单位的时间,求建1、2...、(n+1)/2座山分别需要的耗费的最小时间。
思路:状态dp[i][j][0/1]前i座山选j个其中第i个山是否选择
dp[i][j][0] = min(dp[i-1][j][0], dp[i-1][j][1])
dp[i][j][1] = min(dp[i][j-1][0]+max(a[i-1]-a[i]+1, 0), dp[i][j-1][1] + max(a[i-1]-min(a[i], a[i-2])+1, 0));//因为第i座选择的话降低i-1座山是被第i-2座山是否选择所影响。
可以发现第i个状态是有i-1和i-2转移过来,可以考虑滚动数组优化
#include <bits/stdc++.h>
using namespace std;
#define N 5100
#define ll long long
#define INF 0x3f3f3f3f
int a[N];
int dp[3][N<<1][2];
int n, t;
int main ()
{
while(scanf("%d", &n)!=EOF) {
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
int k = (n+1)/2;
memset(dp, INF, sizeof(dp));
for(int i = 0; i <= 2; ++i) {
dp[i][0][0] = 0;
}
t = 1;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= k; ++j) {
if(i == 1) {
dp[t][j][0] = dp[(t-1+3)%3][j][0];
dp[t][j][1] = dp[(t-1+3)%3][j-1][0];
continue;
}
int t1 = (t-1+3)%3, t2 = (t-2+3)%3;
dp[t][j][0] = min(dp[t1][j][0], dp[t1][j][1]+max(a[i]-a[i-1]+1, 0));
dp[t][j][1] = min(dp[t2][j-1][0]+max(a[i-1]-a[i]+1, 0), dp[t2][j-1][1] + max(a[i-1]-min(a[i], a[i-2])+1, 0));
}
t = (t+1)%3;
}
t = (t-1+3)%3;
for(int i = 1; i <= k; ++i) {
printf("%d%c", min(dp[t][i][0], dp[t][i][1]), i==k?'\n':' ');
}
}
return 0;
}