codeforces 1012c Hills dp

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值