小白算法练习 简单背包问题专题004 多重背包 二进制化 POJ dp
来源:互联网 发布:随机抽奖软件 编辑:程序博客网 时间:2024/06/06 00:57
Dividing
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 72022 Accepted: 18801
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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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 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<cstring >using namespace std;typedef long long ll;int main(){int v[7];int w[7];for(int i=1;i<=6;i++){v[i]=i;}int n1,n2,n3,n4,n5,n6;int c=0;while(cin>>n1>>n2>>n3>>n4>>n5>>n6 && ( n1!=0 || n2!=0 || n3!=0 || n4!=0 || n5!=0 || n6!=0) ){ll s=0;w[1]=n1;w[2]=n2;w[3]=n3;w[4]=n4;w[5]=n5;w[6]=n6;s=w[1]*1+w[2]*2+w[3]*3+w[4]*4+w[5]*5+w[6]*6;if(s%2!=0){cout<<"Collection #"<<++c<<":"<<endl;cout<<"Can't be divided."<<endl;cout<<endl;//注意这里的输出格式。。。}else{int count=1;int value[10000];memset(value,0,sizeof(value));for(int i=1;i<=6;i++) //二进制转换{if(w[i]==0) continue;else{for(int j=1;j<=w[i];j=j*2){value[count++]=j*v[i];w[i]-=j;}if(w[i]>0){value[count++]=w[i]*v[i];}}}int dp[300008]; memset(dp,0,sizeof(dp));for(int i=1;i<=count;i++){for(int j=s/2;j>=value[i];j--){dp[j]=max(dp[j],dp[j-value[i]]+value[i]);} }cout<<"Collection #"<<++c<<":"<<endl;if(dp[s/2]==s/2){cout<<"Can be divided."<<endl;}else{cout<<"Can't be divided."<<endl;}cout<<endl;//注意这里的输出格式。。。}}return 0;}
Cash MachineTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 36777 Accepted: 13344Description
A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example, N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10 means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each. Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine. Notes: @ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc.Input
The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: cash N n1 D1 n2 D2 ... nN DN where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct.Output
For each set of data the program prints the result to the standard output on a separate line as shown in the examples below.Sample Input
735 3 4 125 6 5 3 350633 4 500 30 6 100 1 5 0 1735 00 3 10 100 10 50 10 10Sample Output
73563000Hint
The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash.
In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash.
In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.代码
#include<iostream>#include<algorithm>using namespace std;int main(){int cash,N;while( cin>>cash>>N && ( cash!=EOF && N!=EOF) ){int c[11]={0},v[11]={0};for(int i=1;i<=N;i++){cin>>c[i]>>v[i];}int count=0;int value[100008]={0};for(int i=1;i<=N;i++){for(int j=1;j<=c[i];j=j*2){value[count++]=j*v[i];c[i]-=j;}if(c[i]>=0){value[count++]=c[i]*v[i];}}int dp[100008]={0};for(int i=0;i<count;i++){for(int j=cash;j>=value[i];j--){dp[j]=max(dp[j],dp[j-value[i]]+value[i]);} }cout<<dp[cash]<<endl;}}
阅读全文
0 0
- 小白算法练习 简单背包问题专题004 多重背包 二进制化 POJ dp
- 小白算法练习 简单背包问题专题001 hdu 01背包 dp
- 小白算法练习 简单背包专题002 01背包k优解 hdu dp
- 小白算法练习 简单背包专题003 完全背包 hdu lanqiao 包子凑数 dp
- POJ 多重背包专题
- POJ 2392 简单dp 多重背包
- poj 1276 多重背包+二进制
- POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】
- poj 1742 多重背包算法优化问题
- poj 1276 dp多重背包
- POJ 1276 二进制优化的多重背包问题
- POJ-1276-Cash-Machine 二进制优化多重背包问题
- hdu 1059 练习练习dp(多重背包)
- poj 1014 Dividing 多重背包 二进制拆分
- poj-1014-多重背包+二进制优化
- POJ 1014 Dividing(二进制优化+多重背包)
- poj 1276 多重背包+二进制解法
- POJ 1742 多重背包问题
- (十七)SVG 实例-可交互式中国地图
- SuperSocket基础二
- Octave语法
- 排序算法之插入排序(Java)
- 如何自定义注解实现简单的权限控制
- 小白算法练习 简单背包问题专题004 多重背包 二进制化 POJ dp
- P1433 吃奶酪
- 热传播测地线距离的计算
- Spring 容器创建 依赖注入 web整合 注解使用
- C++中的类内存布局②---补充(12)《Effective C++》
- php 实现验证码
- spfa 的优化
- HDU 1394 Minimum Inversion Number(线段树,单节点修改,区间求和)
- Java经典编程—古典兔子