DP练习,九度1453Greedy Tino

来源:互联网 发布:淘宝数据包怎么做 编辑:程序博客网 时间:2024/05/17 16:55

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 2000using 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 2000using 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;}
原创粉丝点击