HDU 1059 Dividing 多重背包
来源:互联网 发布:mysql between 日期 编辑:程序博客网 时间:2024/05/21 01:57
这道题的意思是,有六件物品每件物品的价值依次是1,2,3,4,5,6每件物品的个数各不相同。求所有物品价值是否能够平分,就是总价值的一半是否能由当前的物品的价值组合出来。所以这是一道多重背包的问题。状态转移方程是:
f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k<=n[i]} 枚举所有的情况,如果dp[w/2]可以找到的话那就满足条件、、
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13377 Accepted Submission(s): 3754
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.
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.
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.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 01 0 0 0 1 10 0 0 0 0 0
Sample Output
Collection #1:Can't be divided.Collection #2:Can be divided.
#include <stdio.h>#include <iostream>#include <string.h>using namespace std;int main(){ int dp[101000], i, j, t; int num[10], k, sum, case1 = 1; while(cin >>num[1]) { sum = num[1]; for(i = 2; i <= 6; i++) { cin >>num[i]; sum += num[i]*i; } if(sum == 0) break; if(sum%2) { printf("Collection #%d:\nCan't be divided.\n\n",case1++); continue; } t = sum/2; for(i = 1; i <= t; i++) dp[i] = 0; for(i = 0; i <= num[1] && i <= t; i++) dp[i] = 1; for(i = 2; i <= 6; i++) { for(j = t; j >= 0; j--) { if(!dp[j]) continue; for(k = 1; k <= num[i] && k <= t; k++) { if(dp[k*i+j]) break; dp[k*i+j] = 1; } } } if(dp[t]) printf("Collection #%d:\nCan be divided.\n\n",case1++); else printf("Collection #%d:\nCan't be divided.\n\n",case1++); } 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 多重背包
- TCP/IP三次握手原理
- 我为什么叫李思家
- 技术普及帖:你刚才在淘宝上买了一件东西
- Linux 命令操作之 如何运行 SH文件
- 最短路
- HDU 1059 Dividing 多重背包
- gridview 属性
- 在Eclipse中配置tomcat
- poj 3259 Wormholes
- __declspec(dllexport) & __declspec(dllimport)
- android用于打开各种文件的intent
- 各种oracle10g下载地址(官网网页上好像找不到了)
- 程序员面试之道(《程序员面试笔试宝典》)之快乐编程的好习惯有哪些?
- InvalidateRect,ValidateRect,WM_PAINT消息