Hdu 1059 Dividing(dp)

题目链接

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19596    Accepted Submission(s): 5514


Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
 

Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000.

The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
 

Output
For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''.

Output a blank line after each test case.
 

Sample Input
  
  
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
 

Sample Output
  
  
Collection #1: Can't be divided. Collection #2: Can be divided.
题意:已知有价值为1,2,3,4,5,6的硬币,以及每种硬币的数量n1,n2,n3.......n6。问是否能将这些钱分成相同的两部分。

题解:

方法一:典型的多重背包问题,可以多重背包O(n*v)的方法来做(用单调队列优化)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<string.h>
#include<string>
#include<stdlib.h>
typedef long long LL;
typedef unsigned long long LLU;
const int nn=110000;
const int inf=0x3fffffff;
const LL inf64=(LL)inf*inf;
const int mod=1000000007;
using namespace std;
int a[10];
int dp[nn*10];
pair<int,int>dq[7][nn*5];
int head[7],last[7];
int main()
{
    int i,j;
    int cas=1;
    while(1)
    {
        int sum=0;
        for(i=1;i<=6;i++)
        {
            scanf("%d",&a[i]);
            sum+=i*a[i];
        }
        if(sum==0)
            break;
        printf("Collection #%d:\n",cas++);
        if(sum&1)
        {
            puts("Can't be divided.");
            puts("");
            continue;
        }
        dp[0]=0;
        for(i=1;i<=sum/2;i++)
        {
            dp[i]=-inf;
        }
        pair<int,int>tem;
        for(i=1;i<=6;i++)
        {
            for(j=0;j<i;j++)
                head[j]=last[j]=0;
            for(j=0;j<=sum/2;j++)
            {
                if(dp[j]!=-inf)
                {
                    while(head[j%i]<last[j%i])
                    {
                        tem=dq[j%i][last[j%i]-1];
                        if(tem.second<=dp[j]-j/i*i)
                            last[j%i]--;
                        else
                            break;
                    }
                    dq[j%i][last[j%i]++]=(make_pair(j/i,dp[j]-j/i*i));
                }
                while(head[j%i]<last[j%i])
                {
                    tem=dq[j%i][head[j%i]];
                    if(tem.first<j/i-a[i])
                        head[j%i]++;
                    else
                    {
                        dp[j]=max(dp[j],tem.second+j/i*i);
                        break;
                    }
                }
            }
        }
        if(dp[sum/2]==sum/2)
            puts("Can be divided.");
        else
            puts("Can't be divided.");
        puts("");
    }
    return 0;
}


方法二:相对于多重背包问题而言,这题还有个特殊的性质,即物品的体积等于物品的价值。

那么我们可以用 dp[i][j] 表示前i种硬币,组成价值j是否可行,转移就是:

dp[i][j]=dp[i-1][j-k*i]?true:dp[i][j]。0<=k<=a[i] 。

转移和多重背包转移类似,同样我们可以用优化多重背包转移的原理,剩余类优化。由于我们只考虑可行性,所以我们只用维护这一类的可行的最大值,而不用维护单调队列。同时第一维空间也可以优化掉。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<string.h>
#include<vector>
#include<math.h>
typedef long long LL;
typedef unsigned long long LLU;
const double eps=1e-8;
const int nn=110000;
const int inf=0x3fffffff;
using namespace std;
int a[10];
bool dp[nn*10];
int f[10];
int main()
{
    int i,j;
    int cas=1;
    while(1)
    {
        int sum=0;
        for(i=1;i<=6;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i]*i;
        }
        if(sum==0)
            break;
        printf("Collection #%d:\n",cas++);
        if(sum&1)
        {
            puts("Can't be divided.");
            puts("");
            continue;
        }
        dp[0]=true;
        for(i=1;i<=sum/2;i++)
            dp[i]=false;
        for(i=1;i<=6;i++)
        {
            for(j=0;j<i;j++)
                f[j]=-inf;
            for(j=0;j<=sum/2;j++)
            {
                if(dp[j])
                {
                    f[j%i]=j/i;
                }
                else if(f[j%i]>=j/i-a[i])
                {
                    dp[j]=true;
                }
            }
        }
        if(dp[sum/2])
            puts("Can be divided.");
        else
            puts("Can't be divided.");
        puts("");
    }
    return 0;
}
方法三:相对于多重背包而言,此题的价值和体积相等,利用这个性质。

我们用 dp[i][j]表示前i种硬币,组成价值j,最多剩了多少个i物品。转移就是

如果dp[i-1][j]可行,则:

dp[i][j]=ni ;

如果dp[ i ][ j-i ]>=1,则:

dp[i][j]=dp[i][j-i]-1;

代码如下

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<string.h>
#include<vector>
#include<math.h>
typedef long long LL;
typedef unsigned long long LLU;
const double eps=1e-8;
const int nn=110000;
const int inf=0x3fffffff;
using namespace std;
int a[10];
int dp[nn*10];
int main()
{
    int i,j;
    int cas=1;
    while(1)
    {
        int sum=0;
        for(i=1;i<=6;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i]*i;
        }
        if(sum==0)
            break;
        printf("Collection #%d:\n",cas++);
        if(sum&1)
        {
            puts("Can't be divided.");
            puts("");
            continue;
        }
        dp[0]=0;
        for(i=1;i<=sum/2;i++)
            dp[i]=-1;
        for(i=1;i<=6;i++)
        {
            for(j=0;j<=sum/2;j++)
            {
                if(dp[j]!=-1)
                {
                    dp[j]=a[i];
                }
                else if(j-i>=0&&dp[j-i]>=1)
                {
                    dp[j]=dp[j-i]-1;
                }
            }
        }
        if(dp[sum/2]!=-1)
            puts("Can be divided.");
        else
            puts("Can't be divided.");
        puts("");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值