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
题目大意:有一列山,h[i]表示第i个山的高度,如果在第i个山上建房子,那么必须满足i两侧的山的高度比i低,一次操作可以使得一座山的高度减一,现在问如果要建1,2......(n / 2)个房子各最少需要多少次操作。
dp
dp[i][j][0]表示前i座山建j个房子,并且第i座山不建房子的最少操作数,可以由dp[i-1][j][0]直接转移过来,或者由dp[i-1][j][1]状态转移来,此时如果第i座山高于i-1座山,那么需要对i座山进行操作使得dp[i-1][j][1]成立。
dp[i][j][1]表示前i座山建j个房子,并且第i座山建房子的最少操作数。可以由dp[i-2][j-1][0]转移过来,此时需要对第i-1座山进行操作使得他的高度小于第i座山;或者由dp[i-2][j-1][1]转移过来,此时需要对第i-1座山进行操作,使得他的高度小于i-2和i的高度。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 5e3 + 10;
const int INF = 1e9;
int dp[N][N][2];
int num[N], n;
int cost(int a, int b) {
return a > b ? 0 : b - a + 1;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dp[i][j][0] = dp[i][j][1] = INF;
dp[1][1][1] = 0;
dp[2][1][1] = cost(num[2], num[1]);
dp[2][1][0] = cost(num[1], num[2]);
for (int i = 3; i <= n; i++) {
for (int j = 1; j <= i; j++) {
dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1] + cost(num[i - 1], num[i]));
dp[i][j][1] = min(dp[i - 2][j - 1][0] + cost(num[i], num[i - 1]),
dp[i - 2][j - 1][1] + max(cost(num[i], num[i - 1]), cost(num[i - 2], num[i - 1])));
if (j * 2 >= i) break;
}
}
for (int i = 1; i <= n; i++) {
printf("%d ", min(dp[n][i][0], dp[n][i][1]));
if (i * 2 >= n) break;
}
printf("\n");
return 0;
}