这个题应该是一个使用背包算法的题,但是就不到10天的时间,背包算法看了一眼,但觉得得不到最友答案啊,拟阵什么的看不懂的说。平分财产:看到一个适合我的算法,当初我也想过像背包一样分,但是绕不过最优解。这里将所以的物品按价值从大到小依次排列,对于加上就超过一半的情况就跳过,取下一个更小的值,如果还超过一半,依次类推,最后可以得到最优解。
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27778 Accepted Submission(s): 9788
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).
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.
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
20 10 40 40
#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;
<span style="color:#ff6666;">while(num--)
a[len++] = value;</span>
}
sort(a,a+len);
int half = sum / 2;
num = 0;
for(int i=len-1;i>=0;i--){
<span style="color:#ff0000;"> if(num + a[i] > half)
continue;</span>
num += a[i];
}
cout << sum - num << " " << num << endl;
}
return 0;
}
----------------------------------------------------------------------------------