R - Dividing 多重背包
来源poj1059
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.
把让你分成两堆价值相同的石子,把他的价值也当做消耗去做
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=5e5+10;
int dp[N];
int num[7];
int main()
{
int cas=1;
while(1)
{
int sum=0;
rep(i,1,7)
{
scf(num[i]);
sum+=i*num[i];
}
if(sum==0) return 0;
if(sum&1)
{
pf("Collection #%d:\nCan't be divided.\n\n",cas++); continue;
}
sum/=2;
int cnt=0;
mm(dp,0);
dp[0]=1;
rep(i,1,7)
{
if(!num[i]) continue;
for(int j=1;j<=num[i];j*=2)//二进制优化
{
num[i]-=j;
int ans=j*i;
per(k,sum,ans)
if(dp[k-ans])
dp[k]=1;
}
int ans=num[i]*i;
if(ans)
per(j,sum,ans)
if(dp[j-ans])
dp[j]=1;
}
if(dp[sum])
pf("Collection #%d:\nCan be divided.\n\n",cas++);
else
pf("Collection #%d:\nCan't be divided.\n\n",cas++);
}
}
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=5e5+10;
int dp[N];
int tot;
int num[7];
void bag01(int cost,int val)
{
per(i,tot,cost)
dp[i]=max(dp[i],dp[i-cost]+val);
}
void bagall(int cost,int val)
{
rep(i,cost,tot+1)
dp[i]=max(dp[i],dp[i-cost]+val);
}
void multbag(int cost,int val,int n)
{
if(cost*n>tot)
{
bagall(cost,val);
return;
}
int k=1;
while(k<n)
{
n-=k;
bag01(k*cost,k*val);
k*=2;
}
bag01(n*cost,n*val);
}
int main()
{
int cas=1;
while(1)
{
tot=0;
rep(i,1,7)
{
scf(num[i]);
tot+=i*num[i];
}
if(tot==0) return 0;
if(tot&1)
{
pf("Collection #%d:\nCan't be divided.\n\n",cas++); continue;
}
tot/=2;
mm(dp,0);
rep(i,1,7)
{
if(!num[i]) continue;
multbag(i,i,num[i]);
}
if(tot==dp[tot])
pf("Collection #%d:\nCan be divided.\n\n",cas++);
else
pf("Collection #%d:\nCan't be divided.\n\n",cas++);
}
}
R - Dividing 多重背包的更多相关文章
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
- Dividing 多重背包 倍增DP
Dividing 给出n个物品的价值和数量,问是否能够平分.
- hdu 1059 Dividing 多重背包
点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- hdu1059 Dividing ——多重背包
link:http://acm.hdu.edu.cn/showproblem.php?pid=1059 最简单的那种 #include <iostream> #include <cs ...
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...
- poj1014 hdu1059 Dividing 多重背包
有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...
随机推荐
- JSP(8)—EL案例和JSTL案例
1.EL案例 el.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- Servlet(5)—ServletRequest接口和ServletResponse接口
ServletRequest接口: 使用ServletRequest接口创建对象,用于使客户端请求信息对Servlet可用,创建的对象作为参数传递之Servlet的Service() ServletR ...
- txt2xls
#!/bin/env python# -*- encoding: utf-8 -*-import datetimeimport timeimport osimport sysimport openpy ...
- 【经典】5种IO模型 | IO多路复用
上篇回顾:静态服务器+压测 3.2.概念篇 1.同步与异步 同步是指一个任务的完成需要依赖另外一个任务时,只有等待被依赖的任务完成后,依赖的任务才能算完成. 异步是指不需要等待被依赖的任务完成,只是通 ...
- 使用scp命令在多个Linux系统间进行文件复制
一,什么是scp scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令.scp命令可以在linux服务器之间复制文件和目录.scp使用ssh安全协议传输数据,具有和ssh一样的验证机制,从 ...
- 转: 关于linux用户时间与系统时间的说明
写的很不错的一篇. https://blog.csdn.net/mmshixing/article/details/51307853
- mysql函数之SUBSTRING_INDEX(str,"/",-1)
SUBSTRING_INDEX的用法: •SUBSTRING_INDEX(str,delim,count) 在定界符 delim 以及count 出现前,从字符串str返回自字符串.若count为正值 ...
- 【转】ASP.NET中验证控件的使用
前言: 前几日,无奈用JS判断控件的有效性,发现的确是一件费力.费神的事情!特别是针对邮件格式.邮政编码等的关于正则表达式的JS验证(其中涉及正则表达式的比较等,较烦~).其实对于这些常用的控件有效性 ...
- 使用 Docker 镜像构建 GO 语言环境
1. 安装 Docker 我当前使用的系统环境是 CentOS7 ,安装 Docker 使用的命令是 yum install docker*.至于其它系统,可以到百度查找其对应的安装方式. 2. 配置 ...
- 【PHP】PHP的安装和配置
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广泛,主要适用于W ...