您的位置:首页 > 其它

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

2018-03-31 23:12 429 查看
Problem T Time Limit : 2000/1000ms (Java/Other)   Memory Limit :65536/32768K (Java/Other)Total Submission(s) : 1   Accepted Submission(s) : 1Problem DescriptionMarsha 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>  InputEach 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>  OutputFor 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 Input1 0 1 2 0 01 0 0 0 1 10 0 0 0 0 0  Sample OutputCollection #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("Coll
a02c
ection #%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;
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: