Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 18190    Accepted Submission(s): 5080

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

一开始还觉得有点难写。。

后来AC才感觉自己编码能力已经很强了。。

主要是这个拆分函数就OK了

void solve()
{
for(int i=1;i<=6;i++)
{
int temp=0;
while(n[i]>0)
{
if(n[i]>=ER[temp])
A[++tot]=i*ER[temp];
else
A[++tot]=i*n[i];
n[i]=n[i]-A[tot]/i;
temp++;
}
}
}

完整代码如下:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define MAX 2100000000
using namespace std;
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int n[10];
int F[100001];
int ER[100];
int K;
int A[600];
int tot=0; //总数
void YCL()
{
memset(F,0,sizeof(F));
tot=0;
K=0;
for(int i=1;i<=6;i++)
{
K+=i*n[i];
}
for(int i=1;i<=K/2;i++)
F[i]=MAX;
F[0]=0;
}
void ER1()
{
ER[0]=1;
for(int i=1;i<=15;i++)
{
ER[i]=ER[i-1]*2;
}
}
void solve()
{
for(int i=1;i<=6;i++)
{
int temp=0;
while(n[i]>0)
{
if(n[i]>=ER[temp])
A[++tot]=i*ER[temp];
else
A[++tot]=i*n[i];
n[i]=n[i]-A[tot]/i;
temp++;
}
}
}
int main()
{
// init();
int CASE=0;
ER1(); //得到二的倍数
while(cin>>n[1]>>n[2]>>n[3]>>n[4]>>n[5]>>n[6]&&(n[1]||n[2]||n[3]||n[4]||n[5]||n[6]))
{
CASE++;
YCL(); //计算K
solve(); //拆分开始
if(K%2==0)
{
for(int i=1;i<=tot;i++)
for(int j=K/2;j>=0;j--)
{
if(j-A[i]>=0)
F[j]=min(F[j],F[j-A[i]]);
}
printf("Collection #%d:\n",CASE);
if(F[K/2]!=MAX)
{
printf("Can be divided.\n\n");
}
else
{
printf("Can't be divided.\n\n");
}
}
else
{
printf("Collection #%d:\n",CASE);
printf("Can't be divided.\n\n");
}
}
return 0;
}

恩 再补一道同类型的题目

HDU(3732)

Ahui Writes Word

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2214    Accepted Submission(s): 811

Problem Description
We all know that English is very important, so Ahui strive for this in order to learn more English words. To know that word has its value and complexity of writing (the length of each word does not exceed 10 by only lowercase letters), Ahui wrote the complexity
of the total is less than or equal to C.

Question: the maximum value Ahui can get.

Note: input words will not be the same.
 
Input
The first line of each test case are two integer N , C, representing the number of Ahui’s words and the total complexity of written words. (1 ≤ N ≤ 100000, 1 ≤ C ≤ 10000)

Each of the next N line are a string and two integer, representing the word, the value(Vi ) and the complexity(Ci ). (0 ≤ Vi , Ci ≤ 10)
 
Output
Output the maximum value in a single line for each test case.
 
Sample Input
5 20
go 5 8
think 3 7
big 7 4
read 2 6
write 3 5
 
Sample Output
15
Hint
Input data is huge,please use “scanf(“%s”,s)”
 

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
int N,C;
char buffer[30];
int map[20][22];
int ER[30];
int tot;
int v[3000];
int w[3000];
int F[10001];
void ER1()
{
ER[0]=1;
for(int i=1;i<=20;i++)
{
ER[i]=ER[i-1]*2;
}
}
void input()
{
int a,b;
tot=0;
memset(map,0,sizeof(map));
memset(F,0,sizeof(F));
for(int i=1;i<=N;i++)
{
scanf("%s %d %d",buffer,&a,&b);
map[a][b]++;
}
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
void solve()
{
for(int i=1;i<=10;i++)
for(int j=0;j<=10;j++)
{
int temp=0;
while(map[i][j]>0)
{
if(map[i][j]>=ER[temp])
{
v[++tot]=ER[temp]*i;
w[tot]=ER[temp]*j;
}
else
{
v[++tot]=map[i][j]*i;
w[tot]=map[i][j]*j;
}
{
map[i][j]=map[i][j]-v[tot]/i;
temp++;
}
}
}
}
void dp()
{
for(int i=1;i<=tot;i++)
for(int j=C;j>=0;j--)
{
if(j-w[i]>=0)
F[j]=max(F[j],F[j-w[i]]+v[i]);
}
}
int main()
{
// init();
ER1();
while(cin>>N>>C)
{
input();
solve();
dp();
printf("%d\n",F[C]);
}
return 0;
}

【二进制拆分多重背包】【HDU1059】【Dividing】的更多相关文章

  1. poj1014二进制优化多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53029   Accepted: 13506 Descri ...

  2. [Bzoj 1192][HNOI2006]鬼谷子的钱袋(二进制优化多重背包)

    (人生第一篇bzoj题解有点激动 首先介绍一下题目: 看它题目那么长,其实意思就是给定一个数a,求将其拆分成n个数,通过这n个数可以表示出1~a中所有数的方案中,求最小的n. 您看懂了嘛?不懂咱来举个 ...

  3. 51nod 1086 背包问题 V2(二进制优化多重背包)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1086 题解:怎么用二进制优化多重背包,举一个例子就明白了. ...

  4. POJ - 1276 二进制优化多重背包为01背包

    题意:直接说数据,735是目标值,然后3是后面有三种钱币,四张125的,六张五块的和三张350的. 思路:能够轻易的看出这是一个多重背包问题,735是背包的容量,那些钱币是物品,而且有一定的数量,是多 ...

  5. 2018.09.08 bzoj1531: [POI2005]Bank notes(二进制拆分优化背包)

    传送门 显然不能直接写多重背包. 这题可以用二进制拆分/单调队列优化(感觉二进制好写). 所谓二进制优化,就是把1~c[i]拆分成20,21,...2t,c[i]−2t+1+1" role= ...

  6. dp之多重背包hdu1059

    题意:价值为1,2,3,4,5,6. 分别有n[1],n[2],n[3],n[4],n[5],n[6]个.求能否找到满足价值刚好是所有的一半的方案. 思路:简单的多重背包,我建议多重背包都用二进制拆分 ...

  7. 【二进制优化-多重背包】zznu-oj-2120 : 安详--如何用尽钱币打赏主播获得最大好感度

    2120 : 安详 题目描述 spring最近喜欢上了B站新秀主播,身为顿顿吃黄焖鸡的土豪,当然要过去打赏一番,但是spring还是喜欢精打细算,所以在打赏的时候,想要掏出有限的钱,获得主播的最大好感 ...

  8. poj 1742 Coins(二进制优化多重背包)

    传送门 解题思路 多重背包,二进制优化.就是把每个物品拆分成一堆连续的\(2\)的幂加起来的形式,然后把最后剩下的也当成一个元素.直接类似\(0/1\)背包的跑就行了,时间复杂度\(O(nmlogc) ...

  9. BZOJ 1531 二进制优化多重背包

    思路: 讲道理我应该写单调队列优化多重背包的 但是我不会啊 但是我现在! 还不会啊 我就写了个二进制优化的.. 过了 //By SiriusRen #include <cstdio> #i ...

随机推荐

  1. WIN7 64位系统安装JDK并配置环境变量

    本文来自:http://jingyan.baidu.com/article/3c343ff70bc6ea0d377963df.html 工具/原料 JDK 方法/步骤   首先,下载JDK安装包,到官 ...

  2. Android项目实战-云词典

    前段时间在网上看到滴答滴答的一个项目,字典文章,找到一个简单的字.整句翻译功能,词汇,但漫长的岁月项目,二手API数据不再可用,我决定使用其现有的框架来实现其功能,主要采用的技术GSON和Volley ...

  3. CATransform3D参数的意义

    经常忘记CATransform3D各参数的意思,记下来好好理解下   struct CATransform3D { CGFloat m11(x缩放),m12(y切变),m13(旋转),m14(); C ...

  4. ubuntu桌面环境配置及切换

    修改ubuntu默认启动的桌面环境:Ubuntu是否启动图形化界面取决于default-display-manager的设置. vi /etc/X11/default-display-manager值 ...

  5. synchronized关键字的用法

    synchronized用于给方法或者块加锁用的,只有获得该对象或者块的锁的对象才能够执行里面的代码,否则将阻塞在那里,等待该锁被释放,然后获得该锁继续执行.比如下面模拟售票的代码: /** * 模拟 ...

  6. C#.NET面向对象(语法点)

    一.继承 C#中继承的规则 1:继承是可传递的 A:B   B:C 2:派生类应当是对基类的扩展.派生类可以添加新的成员,但不能除去已经继承的成员的定义. 3:构造函数和析构函数不能被继承 4:如果派 ...

  7. spring 配置和实例

    Spring 是一个开源框架.Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring 是一个 IOC(DI) ...

  8. PNG图片小结

    PNG: 便携式网络图形(Portable Network Graphics,PNG)是一种无损压缩的位图图形格式,支持索引.灰度.RGB三种颜色方案以及Alpha通道等特性.PNG的开发目标是改善并 ...

  9. Android sample 之模拟重力感应,加速度

    class SimulationView extends View implements SensorEventListener { // diameter of the balls in meter ...

  10. PHP_CURL请求教程, 内含简单粗暴curl

    //curl访问 //需要url或者data //返回的数组是JSON数据形式 function ppd_curl($url,$data = null){ //\Think\Log::record($ ...