ZOJ 2059 The Twin Towers

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ 2059 The Twin Towers相关的知识,希望对你有一定的参考价值。

双塔DP。

dp[i][j]表示前i个物品,分成两堆(可以不全用),价值之差为j的时候,较小一堆的价值为dp[i][j]。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

int dp[105][4000 + 10];
int a[105];
int n, sum;

void read()
{
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
}

void work()
{
    sum = 0;
    for (int i = 1; i <= n; i++) sum = sum + a[i];
    memset(dp, -1, sizeof dp);
    int h; h = a[1];
    dp[1][h + sum] = dp[1][0 - h + sum] = dp[1][sum] = 0;
    for (int i = 2; i <= n; i++)
    {
        h = a[i];
        for (int j = 0; j <= sum*2; j++) dp[i][j] = dp[i - 1][j];
        for (int j = 0; j <= sum*2; j++)
        {
            if (dp[i - 1][j] == -1) continue;

            int tmp = j - sum;
            if (tmp >= 0)
            {
                dp[i][h + tmp + sum] = max(dp[i][h + tmp + sum], dp[i - 1][j]);
                dp[i][tmp - h + sum] = max(dp[i][tmp - h + sum], dp[i - 1][j] + min(tmp, h));
            }
            else if (tmp<0)
            {
                tmp = -tmp;
                dp[i][0 - (tmp + h) + sum] = max(dp[i][0 - (tmp + h) + sum], dp[i - 1][j]);
                dp[i][h - tmp + sum] = max(dp[i][h - tmp + sum], dp[i - 1][j] + min(tmp, h));
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) ans = max(ans, dp[i][sum]);
    if (ans == 0) printf("Sorry\n");
    else printf("%d\n", ans);
}

int main()
{
    while (~scanf("%d", &n))
    {
        read();
        if (n < 0) break;
        if (n <=1) printf("Sorry\n");
        else work();
    }
    return 0;
}

 

以上是关于ZOJ 2059 The Twin Towers的主要内容,如果未能解决你的问题,请参考以下文章

[zoj] 3229 Shoot the Bullet || 有源汇上下界最大流

代写HTML5 JavascriptWEB作业代写JSPASPAsp.net作业A simple animation Towers of Hanoi

The Towers of Hanoi Revisited---(多柱汉诺塔)

Acdream 1219 The Towers of Hanoi Revisited(递归汉诺塔问题)

Navicate连接MySQL出现2059-Authentication plugin 'caching_sha2_password' cannot be loaded:的解决方案(代

SQLyog连接MySQL8 异常2059-Authentication plugin 'caching_sha2_password' cannot be loaded解决方案(示例代