码迷,mamicode.com
首页 > 其他好文 > 详细

Divideing Jewels(nyoj546)(多重背包+二进制优化)

时间:2014-05-21 22:05:03      阅读:492      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   c   code   

Divideing Jewels

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述

Mary and Rose own a collection of jewells. They want to split the collection among themselves so that both receive an equal share of the jewels. This would be easy if all the jewels had the same value, because then they could just split the collection in half. But unfortunately, some of the jewels are larger, or more beautiful than others. So, Mary and Rose start by assigning a value, a natural number between one and ten, to each jewel. Now they want to divide the jewels so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the jewels in this way (even if the total value of all jewels is even). For example, if there are one jewel 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 jewels.

输入
Each line in the input file describes one collection of jewels to be divided. The lines contain ten non-negative integers n1 , . . . , n10 , where ni is the number of jewels of value i. The maximum total number of jewells will be 10000.
The last line of the input file will be "0 0 0 0 0 0 0 0 0 0"; do not process this line.
输出
For each collection, output "#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.
样例输入
1 0 1 2 0 0 0 0 2 0
1 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
样例输出
#1:Can‘t be divided.

#2:Can be divided.


这题是一个背包题,很类似于hdu1059,大致的意思就是给出10个数值,标志该下标的数字有多少个,下标为该类物品的价值,求是否能将所有物品分为两组价值相同的物品
如果能输出“can be divided.”
刚开始一看简单背包题,拿过来就写,三个循环,果不其然的超时了
错误的代码:
bubuko.com,布布扣
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<string.h>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<vector>
 8 #include<cstring>
 9 #include<stack>
10 #include<stdlib.h>
11 #include<ctype.h>
12 using namespace std;
13 #define MAXN 1300
14 #define inf 1000000
15 
16 
17 int a[11],dp[50001];
18 int main()
19 {
20     int flag=1;
21     while(1)
22     {
23         int sum=0,k=0;
24         for(int i=1;i<11;i++)
25         {
26             cin>>a[i];
27             sum+=(a[i]*i);
28             k+=a[i];
29         }
30         if(k==0)break;
31         if(sum%2!=0){printf("#%d:Can‘t be divided.\n\n",flag++);continue;}
32         sum/=2;
33         for(int i=0;i<sum+1;i++)
34             dp[i]=0;
35         for(int i=1;i<11;i++)
36             for(int j=1;j<=a[i];j++)
37             for(int k=i;k<=sum;k++)
38         {
39             dp[k]=max(dp[k],dp[k-i]+j*i);
40         }
41         if(dp[sum]==sum)
42             printf("#%d:Can be divided.\n\n",flag++);
43         else
44             printf("#%d:Can‘t be divided.\n\n",flag++);
45 
46     }
47 }
bubuko.com,布布扣

后来看看,发现这题怎么那么眼熟,看自己以前的博客(csdn)发现,确实是类型题,所以就改下数据,水过了!

表示这个可以当模板用啊!

代码:

bubuko.com,布布扣
 1     #include<iostream>
 2     #include<stdio.h>
 3     #include<string.h>
 4     #include<map>
 5     #include<vector>
 6     #include<set>
 7     #include<string>
 8     #include<algorithm>
 9     #include<cmath>
10     using namespace std;
11     #define Max(a,b) a>b?a:b
12     #define  Min(a,b)  a<b?a:b
13     #define inf 99999999
14     int dp[50020],num[11],sum;
15 
16     void zero_one(int cost,int weight)//01背包
17     {
18         for(int j=sum;j>=cost;j--)
19         {
20             dp[j]=Max(dp[j],dp[j-cost]+weight);
21 
22         }
23     }
24     void totle_pack(int cost,int weight)//完全背包
25     {
26         for(int j=0;j<=sum;j++)
27         {
28             if(j>=cost)
29                 dp[j]=Max(dp[j],dp[j-cost]+weight);
30         }
31     }
32     void multiple_pack(int cost,int weight ,int n)//多重背包
33     {
34         if(n*cost>=sum)
35             totle_pack(cost,weight);
36         else
37         {
38             int k=1;
39             while(k<n)
40             {
41                 zero_one(cost*k,weight*k);
42                 n-=k;
43                 k*=2;
44             }
45             zero_one(n*cost,n*weight);
46         }
47 
48     }
49 
50     int main()
51     {
52         int t=1;
53         while(1)
54         {
55             sum=0;
56             for(int i=1;i<=10;i++)
57             {
58                 cin>>num[i];
59                 sum+=num[i]*i;
60             }
61             if(sum==0)break;
62 
63             cout<<"#"<<(t++)<<:;
64             if(sum%2==1)
65             {
66                 cout<<"Can‘t be divided."<<endl<<endl;
67             }
68             else
69             {
70                 sum/=2;
71                 for(int i=0;i<=sum;i++)
72                     dp[i]=0;
73                 for(int i=1;i<=10;i++)
74                 {
75                     if(num[i])
76                     multiple_pack(i,i,num[i]);
77                 }
78                 if(dp[sum]==sum)
79                     cout<<"Can be divided."<<endl<<endl;
80                 else
81                     cout<<"Can‘t be divided."<<endl<<endl;
82             }
83         }
84         return 0;
85     }
bubuko.com,布布扣

 

Divideing Jewels(nyoj546)(多重背包+二进制优化),布布扣,bubuko.com

Divideing Jewels(nyoj546)(多重背包+二进制优化)

标签:des   style   blog   class   c   code   

原文地址:http://www.cnblogs.com/zhaopengze/p/3739223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!