HDU-1059 Dividing(多重背包)
来源:互联网 发布:ar涂涂秀软件下载 编辑:程序博客网 时间:2024/06/05 08:14
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27713 Accepted Submission(s): 7964
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.
多重背包
分为2块, 每拿一个物品遍历一次;
1. 如果当前所拿物品总重量大于背包容量,那么相当于无限取, 即为完全背包
2. 2.如果当前所拿物品总重量小于背包容量, 相当于01背包, 即 将1个物品的多个 拆分成1个一个
01 背包处理时候 暴力会时间超限,难受, 只能换了个方法
code:
#include<stdio.h>#include<string.h>int a[7];int dp[120005];int v;void ZeroPack(int weight){ for(int i=v;i>=weight;i--) if(dp[i-weight]+weight>dp[i]) dp[i]=dp[i-weight]+weight;}void CompletePack(int weight){ for(int i=weight;i<=v;i++) if(dp[i-weight]+weight>dp[i]) dp[i]=dp[i-weight]+weight;}void MulPack(int weight, int n){ if(weight*n>=v) CompletePack(weight); else { int k=1; while(k<=n) { ZeroPack(k*weight); n=n-k; k=k*2; } ZeroPack(weight*n);// for (int i=1; i<=n; i++)// {// ZeroPack(weight);// } }}int main(){ int sum; int num=0; while(1) { num++; sum=0; for(int i=1;i<7;i++) { scanf("%d",&a[i]); sum+=a[i]*i; } if(sum==0) break; if(sum%2==1) { printf("Collection #%d:\nCan't be divided.\n\n",num); continue; } else { v=sum/2; memset(dp,0,sizeof(dp)); for(int i=1;i<7;i++) MulPack(i,a[i]); //printf ("%d\n",dp[v]); if(dp[v]==v) printf("Collection #%d:\nCan be divided.\n\n",num); else printf("Collection #%d:\nCan't be divided.\n\n",num); } } return 0;}
- hdu 1059 Dividing 多重背包
- hdu 1059 Dividing(多重背包)
- hdu 1059 Dividing(多重背包)
- HDU 1059 Dividing(多重背包)
- hdu 1059 Dividing 多重背包
- Hdu 1059 Dividing -- 多重背包
- hdu 1059 Dividing 多重背包
- HDU-1059-Dividing-多重背包
- HDU-1059 Dividing 多重背包
- hdu 1059 Dividing (多重背包)
- HDU 1059 Dividing 多重背包
- Hdu 1059 Dividing (多重背包)
- HDU 1059 Dividing 多重背包
- hdu-1059-多重背包-Dividing
- HDU 1059 Dividing 多重背包
- HDU 1059 Dividing(多重背包)
- 【多重背包】HDU 1059 Dividing
- hdu 1059 Dividing 多重背包
- ActiveMQ的简单介绍和安装
- typeof()用法及JS基本类型
- mysq数据库管理工具navicat基本使用方法
- 11b、11a/g、11n和11ac速率
- talib 使用 RSI
- HDU-1059 Dividing(多重背包)
- 先获取经纬度 然后再输出
- 正则表达式-问号的四种用法
- ecplise启动时报错问题总结
- see you again
- 算法导论16.1-2
- let和const
- Shader forge 屏幕效果、溶解
- 常见的4种HTML5错误用法,你用错了几个?