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

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.”

刚开始一看简单背包题,拿过来就写,三个循环,果不其然的超时了

错误的代码:


 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 }

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

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

代码:


 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 }

时间: 2024-10-28 10:03:39

Divideing Jewels(nyoj546)(多重背包+二进制优化)的相关文章

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

[多重背包+二进制优化]HDU1059 Dividing

题目链接 题目大意: 两个人要把一堆宝珠,在不能切割的情况下按照价值平分,他们把宝珠分成6种价值,每种价值的宝珠n个. n<=200000 思考: 首先如果加和下来的价值是一个偶数 那么还分毛啊,直接gg. 之后多重背包二进制优化 转换为 01背包. 我们可以把价值 同时当做宝珠的空间和价值. 那么我们现在要求的是 在 空间为一半的情况下,能否找到价值为 一半的情况. 1 #include <cstdio> 2 #include <algorithm> 3 #include

14年省赛---多重部分和问题(多重背包+二进制优化)

1210: F.多重部分和问题 时间限制: 1 Sec  内存限制: 64 MB提交: 18  解决: 14 题目描述 有n种不同大小的数字,每种各个.判断是否可以从这些数字之中选出若干使它们的和恰好为K. 输入 首先是一个正整数T(1<=T<=100)接下来是T组数据 每组数据第一行是一个正整数n(1<=n<=100),表示有n种不同大小的数字 第二行是n个不同大小的正整数ai(1<=ai<=100000)第三行是n个正整数mi(1<=mi<=100000

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given numbers

hdu1059 dp(多重背包二进制优化)

hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听过```不得不吐槽自己的知识面太窄```于是,我用了母函数写这题,母函数的做法并没有问题,但是由于这道题的数据很大,母函数轻轻松松就超时了,于是,我又很努力地在母函数循环的优化上面想出路,改改改,各种改之后依旧TLE,01背包的做法显然也是会超时的,DISCUSS里的母函数做法优化方式都是模上一个大

HDU 2191 悼念512【多重背包+二进制优化】

大意分析: 多重背包,转化为01背包即可 可以用二进制进行优化 代码:(代码没有优化,下题是优化才可过的) 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 105; 7 8 int n, m, tot; 9 int p[2005], h[2005]; 10 int dp[maxn]; 11 int solv

POJ 1014 Dividing【多重背包+二进制优化】

大意: 价值1, 2, 3, ……, 6的物品分别a1, a2, ……, a5, a6件 问能否把这些物品分成两份,使其具有相同的价值(所有物品必须全部用上) 分析: 给个物品有多件,即多重背包 只要看能不能将这些物品拼成   总价值 的 一半就可以了 转化为01背包是用二进制优化,否则会超时 代码: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std;

POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】

Language: Default Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63647   Accepted: 16488 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal

POJ 1276 Cash Machine 多重背包--二进制优化

点击打开链接 Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28337   Accepted: 10113 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount