练习三1021
来源:互联网 发布:linux squid 4.0 安装 编辑:程序博客网 时间:2024/06/18 12:47
Problem UTime Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 16 Accepted Submission(s) : 6
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. <br>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.<br>
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. <br><br>The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.<br>
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.''. <br><br>Output a blank line after each test case.<br>
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.
Statistic |Submit | Back
题意:
给出1,2,3,4,5,6这六个数值的个数,问能否将他们平分为两组数值相同的。
思路:多重背包的问题,背包容量为总数值的一半。注意数值不能直接用i,要赋给数组b[i]=i。
代码:
#include<iostream>
using namespace std;
int max(int x,int y)
{return x>y?x:y;}
int main()
{
int t,n,mj[105],dp[1000005];
double p,pj[105];
cin>>t;
while(t--)
{
cin>>p>>n;
int sum=0;
for(int i=0;i<n;i++)
{
cin>>mj[i]>>pj[i];
pj[i]=1-pj[i];
sum+=mj[i];
}
for(int i=0;i<=sum;i++)
dp[i]=0;
for(int i=0;i<n;i++)
{
double k=1;
for(int j=sum;j>=mj[i];j--)
dp[j]=max(dp[j],dp[j-mj[i]]+mj[i]);
}
cout<<dp[sum]<<endl;
}
return 0;
}
using namespace std;
int max(int x,int y)
{return x>y?x:y;}
int main()
{
int t,n,mj[105],dp[1000005];
double p,pj[105];
cin>>t;
while(t--)
{
cin>>p>>n;
int sum=0;
for(int i=0;i<n;i++)
{
cin>>mj[i]>>pj[i];
pj[i]=1-pj[i];
sum+=mj[i];
}
for(int i=0;i<=sum;i++)
dp[i]=0;
for(int i=0;i<n;i++)
{
double k=1;
for(int j=sum;j>=mj[i];j--)
dp[j]=max(dp[j],dp[j-mj[i]]+mj[i]);
}
cout<<dp[sum]<<endl;
}
return 0;
}
0 0
- 练习三1021
- 练习三 1021
- 练习三
- 数据库练习三
- 数据库练习三答案
- 网工练习(三)
- 网工练习(三)
- Sql语句练习三
- 练习三:字符串倒置
- 练习基础题《三》
- 查询练习(三)
- oracle练习(三)
- 指针练习三
- oracle练习(三)
- 三、Hash练习_1156_1
- MapReduce练习(三)
- Java多线程练习三
- Scala练习(三)
- 【bzoj】1051 &&【poj】2186 Popular Cows Tarjan scc
- 欢迎使用CSDN-markdown编辑器
- linux Java开发工具安装与配置
- HDU 5692 Snacks
- 小K的农场 差分约束
- 练习三1021
- HDU 5694 BD String
- WinPE基础知识
- mina解决粘包,找不到解码器,数据帧重传的问题
- poj 1286 Necklace of Beads(polya 定理)
- qt各版本下载地址
- fragment onViewCreated
- C语言中fclose函数
- 大蒟蒻の留言板