小白算法练习 简单背包问题专题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.

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.

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 Machine
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 36777 Accepted: 13344

Description

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 10

Sample Output

73563000

Hint

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;}}

原创粉丝点击