DP练习,九度1453Greedy Tino

本文介绍了一个扩展的0-1背包问题——平衡载重问题。该问题是关于如何在两根扁担上分配重量相等的橙子,使得总重量最大化。文章详细解释了解决方案,并提供了两种实现方式的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jobdu1453:Greedy Tino

题目

题目描述:

Tino wrote a long long story. BUT! in Chinese…
So I have to tell you the problem directly and discard his long long story. That is tino want to carry some oranges with “Carrying pole”, and he must make two side of the Carrying pole are the same weight. Each orange have its’ weight. So greedy tino want to know the maximum weight he can carry.

输入:

The first line of input contains a number t, which means there are t cases of the test data.
for each test case, the first line contain a number n, indicate the number of oranges.
the second line contains n numbers, Wi, indicate the weight of each orange
n is between 1 and 100, inclusive. Wi is between 0 and 2000, inclusive. the sum of Wi is equal or less than 2000.

输出:

For each test case, output the maximum weight in one side of Carrying pole. If you can’t carry any orange, output -1. Output format is shown in Sample Output.

样例输入:

1

5

1 2 3 4 5

样例输出:

Case 1: 7

分析

此题为扩展的0-1背包问题
dp[m][n]表示在前m个橘子中,两只扁担重量相差重量为n的橘子的最大总重量
另dp值为-1为无效,如拿前m-1个橘子重量差为n的最大重量为value,则拿第m个橘子时(设其重量为vo),有两种选择

1.不拿此橘子
dp[m][n]=dp[m-1][n]
2.拿此橘子
则更新重量差为n+vo和|n-vo|的值为value+vo。
并在每次更新时选择最大值更新

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define MAX 2000

using namespace std;

int dp[MAX][MAX];
int weight[MAX];
int main(){

    int n,t;
    scanf("%d",&t);
    int value;
    bool hz;
    for(int k=0;k<t;k++)
    {
        memset(dp,-1,sizeof(dp));
        scanf("%d",&n);
        dp[0][0]=0;
        hz=false;
        for(int i=0;i<n;i++){
            scanf("%d",&weight[i]);
            if(weight[i]==0) hz=true;
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<MAX;j++){
                if(dp[i-1][j]!=-1){
                    dp[i][j]=max(dp[i-1][j],dp[i][j]);
                    value=j+weight[i-1];

                    if(value<MAX)
                        dp[i][value]=max(dp[i][value],dp[i-1][j]+weight[i-1]);

                    value=j>weight[i-1]?j-weight[i-1]:-j+weight[i-1];
                    dp[i][value]=max(dp[i][value],dp[i-1][j]+weight[i-1]);

                }
            }
        }
        if(dp[n][0]==0)
            printf("Case %d: %d\n",k+1,hz?0:-1);
        else
            printf("Case %d: %d\n",k+1,dp[n][0]/2);
    }
    return 0;
}

优化-滚动数组

每次更新只与上一层的值有关,可以通过滚动数组优化内存

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define MAX 2000

using namespace std;

int dp[2][MAX];

int weight[MAX];

int main(){

    int n,t;
    scanf("%d",&t);
    int value;
    bool hz;
    int *last,*now;
    for(int k=0;k<t;k++)
    {
        memset(dp,-1,sizeof(dp));
        last=dp[0];
        now=dp[1];
        scanf("%d",&n);
        last[0]=0;
        hz=false;
        for(int i=0;i<n;i++){
            scanf("%d",&weight[i]);
            if(weight[i]==0) hz=true;
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<MAX;j++){
                if(last[j]!=-1){
                    now[j]=max(last[j],now[j]);
                    value=j+weight[i-1];

                    if(value<MAX)
                        now[value]=max(now[value],last[j]+weight[i-1]);

                    value=j>weight[i-1]?j-weight[i-1]:-j+weight[i-1];
                    now[value]=max(now[value],last[j]+weight[i-1]);

                }
            }
            swap(last,now);
            memset(now,-1,sizeof(now));
        }
        if(last[0]==0)
            printf("Case %d: %d\n",k+1,hz?0:-1);
        else
            printf("Case %d: %d\n",k+1,last[0]/2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值