标签:des style http java color os
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22623 Accepted Submission(s): 7948
Problem Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don‘t know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is
N (0<N<1000) kinds of facilities (different value, different kinds).
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding
number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that
A is not less than B.
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
Author
lcy
Recommend
We have carefully selected several similar problems for you:
2844 2191 1176 1114 1398
题意:将一组数分成两份,要求相差最小。
多重背包,可转换为 0-1背包 求解,背包容量为总价值的一半。
因为总价值不变,一方的价值如果越靠近总价值的一半,那么双方之间的差距越小。
代码如下:
多重背包转01背包:
#include <iostream>
#include <algorithm>
using namespace std;
#define M 5000 //试过4000,结果挂了。
#define N 250000
int dp[N],vis[M];
int max(int x,int y)
{
return x>y?x:y;
}
int main(int i,int j,int k)
{
int n,tot,x,y;
while(scanf("%d",&n)!=EOF&&n>=0)
{
k=tot=0;
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
tot+=x*y;
while(y--)
{
vis[++k]=x; //多重背包转换01背包。
}
}
for(i=1;i<=k;i++) //01背包模板,物品数量0->k,背包容量sum/2;
for(j=tot/2;j>=vis[i];j--)
dp[j]=max(dp[j],dp[j-vis[i]]+vis[i]); //第i个物品放还是不放的选择。
printf("%d %d\n",tot-dp[tot/2],dp[tot/2]);
}
return 0;
}
百度0MS作品:
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 110000;
int a[MAX],len,n;
int main()
{
while(cin >> n && n >= 0){
memset(a,0,sizeof(a));
int num,value,sum = 0;
len = 0;
for(int i=0;i<n;i++){
cin >> value >> num;
sum += value * num;
while(num--)
a[len++] = value; //在这里应该还是01背包的思想
}
sort(a,a+len); //不明白。。。
int half = sum / 2;
num = 0;
for(int i=len-1;i>=0;i--){
if(num + a[i] > half)
continue; //完全不懂原理了。
num += a[i];
}
cout << sum - num << " " << num << endl;
}
return 0;
}
HDU 1171 Big Event in HDU (动规),布布扣,bubuko.com
HDU 1171 Big Event in HDU (动规)
标签:des style http java color os
原文地址:http://blog.csdn.net/qq2256420822/article/details/38017317