HDU 1059(多重背包加二进制优化)
http://acm.hdu.edu.cn/showproblem.php?pid=1059
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29901 Accepted Submission(s): 8501
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.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output a blank line after each test case.
Can't be divided.
Can be divided.
#include<bits/stdc++.h>
using namespace std;
#define max_v 120000
#define max_n 7
int num[max_n],f[max_v],v[max_n]={0,1,2,3,4,5,6};
void ZeroOnePack(int cost,int weight,int c)
{
for(int v=c;v>=cost;v--)
{
f[v]=max(f[v],f[v-cost]+weight);
}
}
void CompletePack(int cost,int weight,int c)
{
for(int v=cost;v<=c;v++)
{
f[v]=max(f[v],f[v-cost]+weight);
}
}
void MultiplePack(int cost,int weight,int amount,int c)
{
if(cost*amount>=c)
{
CompletePack(cost,weight,c);
return ;
}
int k=1;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight,c);
amount-=k;
k*=2;
}
ZeroOnePack(amount*cost,amount*weight,c);
}
int main()
{
int t=0;
while(1)
{
int sum=0,flag=0;
for(int i=1;i<max_n;i++)
{
scanf("%d",&num[i]);
sum=sum+i*num[i];
}
if(sum==0)
break;
if(sum%2==1)
{
printf("Collection #%d:\nCan't be divided.\n\n",++t);
continue;
}
sum=sum/2;
memset(f,0,sizeof(f));
for(int i=1;i<max_n;i++)
{
MultiplePack(v[i],v[i],num[i],sum);
}
//printf("%d\n",f[sum]);
if(f[sum]==sum)
{
flag=1;
}
if(flag==1)
{
printf("Collection #%d:\nCan be divided.\n\n",++t);
}else
{
printf("Collection #%d:\nCan't be divided.\n\n",++t);
}
}
return 0;
}
HDU 1059(多重背包加二进制优化)的更多相关文章
- 动态规划:HDU2844-Coins(多重背包的二进制优化)
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 1059 (多重背包) Dividing
这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...
- dp之多重背包(二进制优化)
void solve(int v,int w,int c){ int count=0; for(int k=1;k<=c;k<<=1) { val[c ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- luogu||P1776||宝物筛选||多重背包||dp||二进制优化
题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...
- CodeForces922E DP//多重背包的二进制优化
https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...
- HDU-2844 Coins 多重背包 物品数量二进制优化
题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...
- hdu 1059 多重背包
题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 #include<stdio ...
- Dividing (hdu 1059 多重背包)
Dividing Sample Input 1 0 1 2 0 0 价值为1,2,3,4,5,6的物品数目分别为 1 0 1 2 0 0,求能否将这些物品按价值分为两堆,转化为多重背包.1 0 0 0 ...
随机推荐
- Nodejs + express + ejs 之服务器demo
var http=require("http"); var express=require("express"); var fs = require(" ...
- Selenium IDE录制脚本
一.当Selenium IDE中Format没有转换格式时,操作:Options—Options...—勾选Enable experimental features
- 星空灯改装成USB供电
简单的手工活,20分钟搞定 1.用一根USB线剪断,将红黑两根线分别连接到星空灯电源供电的正负极 2.由于USB输出5V 0.5A的电流,因此需要改装下,办法一,加电阻,办法二,换灯泡,由于小电阻并不 ...
- Software Testing Techniques LAB 01: test Junit and Eclemma
1. Installing 1. Install Junit and hamcrest First, I download the Junit-4.12.jar and hamcrest-core- ...
- Linux下部署配置Nginx
1.安装工具包 yum install -y wget 下载工具 yum install -y vim-enhanced vim编辑器 yum install -y make cmake gcc gc ...
- springMVC入门-01
这一系列是在看完网上SpringMVC(基于spring3.0)入门视频之后的个人总结,仅供参考,其中会添加一些个人的见解. 1.搭建SpringMVC所需jar包: org.springframew ...
- 关于Ubuntu16.04下phpmyadmin出现mbstring错误的正解
1.打开终端,输入php -i | grep extension_dir 查看extension_dir的绝对路径 2.查看phpinfo,php.ini的绝对路径 3.打开php.ini文件. 设置 ...
- Zip压缩/解压缩(文件夹)
#PS2.0压缩为.zip文件: $zip = "D:\audit_log\test.zip"New-Item $zip -ItemType file$shellApplicati ...
- Window各种命令补
- python基本数据类型(容器)- tuple list dict set
tuple list dict set统称为“容器” 1. 元组tuple 列表list: 1.1 同:元组( ).列表[ ] 都是有序的 1.2 异:元组不能修改,列表可以修改 1.3 注意⚠:如果 ...