DP(完全背包二进制优化) Problem T:Dividing(HDU 1059)

Problem T

Time Limit : 2000/1000ms (Java/Other)   Memory Limit :65536/32768K (Java/Other)

Total Submission(s) : 1   Accepted Submission(s) : 1

Problem Description

Marsha and Bill own acollection of marbles. They want to split the collection among themselves sothat both receive an equal share of the marbles. This would be easy if all themarbles had the same value, because then they could just split the collectionin half. But unfortunately, some of the marbles are larger, or more beautifulthan others. So, Marsha and Bill start by assigning a value, a natural numberbetween one and six, to each marble. Now they want to divide the marbles sothat each of them gets the same total value. <br>Unfortunately, they realizethat it might be impossible to divide the marbles in this way (even if thetotal value of all marbles is even). For example, if there are one marble ofvalue 1, one of value 3 and two of value 4, then they cannot be split into setsof equal value. So, they ask you to write a program that checks whether thereis a fair partition of the marbles.<br>

 

 

Input

Each line in the inputdescribes one collection of marbles to be divided. The lines consist of sixnon-negative integers n1, n2, ..., n6, where ni is the number of marbles ofvalue i. So, the example from above would be described by the input-line ``1 01 2 0 0''. The maximum total number of marbles will be 20000.<br><br>The last line of the input file will be ``0 0 0 0 0 0''; donot process this line.<br>

 

 

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.''. <br><br>Output ablank line after each test case.<br>

 

 

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.

算法分析:

典型的分组背包,跟上一个题思路基本一样,但这题也叫人郁闷,第一次用这个代码93ms过

代码实现:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,q=0;
    while(1)
    {
        q++;
        int num[102],f[100002],v[100002];
        int sum=0;
        for(int i=1;i<=6;i++)
            {scanf("%d",&num[i]);
            sum+=i*num[i];
            }
          if(sum==0) break;
            //二进制优化
           if(sum%2!=0)
            printf("Collection #%d:\nCan't be divided.\n",q);
           else
            {
                int j=0;
              for(int i=1;i<=6;i++)
            {
               int t=1;
               while(num[i]>=t)
               {
                   j++;
                   v[j]=i*t;
                  num[i]-=t;
                   t*=2;
               }
               j++;
               v[j]=num[i]*i;//将c[i]以2为指数的堆:1,2,4.....2的(k-1)次方,c[i]-(2的k次方)+1
           }
         for(int i=0;i<100002;i++)
            f[i]=0;
            int flag=0;
            for(int i=1;i<=j;i++)
            {for(int k=sum;k>=v[i];k--)
                {f[k]=max(f[k],f[k-v[i]]+v[i]);
                 if(f[k]==sum/2)  {flag=1;break;}
                }
                if(flag==1) break;
            }
           if(flag==1)
             printf("Collection #%d:\nCan't be divided.\n",q);
           else printf("Collection #%d:\nCan be divided.\n",q);
           }
    }
    return 0;
}

但我又优化了一下,竟然没过超时,然后把以前的代码复制过去,结果依旧超时

,杭电的测试系统绝对有毒呢

483ms过的代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,q=0,p;
    while(1)
    {
        q++;
        int num[11],f[100002];
        int sum=0;
        for(int i=1;i<=6;i++)
            {scanf("%d",&num[i]);
            sum+=i*num[i];
            }
          if(sum==0) break;
            //二进制优化
           if(sum%2!=0)
            printf("Collection #%d:\nCan't be divided.\n\n",q);
           else
            {
          int mid=sum/2;
          memset(f,0,sizeof(f));//初始化
          f[0]=1;
          for(int i=1;i<=6;i++)
          {

             int k=1;
              while(num[i]>=k)//二进制优化
              {
                  p=k*i;
                  for(int j=mid;j>=p;j--)
                    if(f[j-p])
                    f[j]=1;
                   num[i]-=k;
                   k*=2;
              }
              if(num[i])
              {
                p=num[i]*i;
                for(int j=mid;j>=p;j--)
                    if(f[j-p])
                    f[j]=1;
              }
          }
           if(f[mid])
             printf("Collection #%d:\nCan be divided.\n\n",q);
           else printf("Collection #%d:\nCan't be divided.\n\n",q);
           }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值