HDU1171 Big Event in HDU 01背包 母函数 TWT Tokyo Olympic 2COMBO-1 未完待续

Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36353    Accepted Submission(s): 12631


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
 

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

Author
lcy


题意:杭电妓院好像分裂了,分成两个学院,现在要分设备,每个设备都有自己的价值,题目会输入每个设备的价值以及每个设备的数量,要求分设备尽可能平均 ,并且输出的两个值A、B中A要大于B


这玩意居然是01背包!?从昨天中午咸鱼到今天中午才有了思路。首先先把所有设备的价值求出来,然后用总价值的一半作为背包的大小,对每个(!)设备看取还是不取。

虽说写出来了。。。但是居然超时了?!啥玩意!?看了看好像没有可以改进的地方,然后就发现自己的数组大小开错了。。笑着觉得真脑残改了改又交

啥玩意?!居然超内存。。。

数据范围:N(N<=50)* v ( v<=50) * m( m<=100) = 250000

看了看数据大小这个数组大小是不能改动的。。看来不能写二维的dp了。。。于是找了题解学了学一维的题解是怎么做的。。。

嗯。。。还是挺好理解的,就是把之前的那个二维循环中的一行拿出来,每次在 i 更新的那个循环中从这个一维数组的尾巴开始对其进行更新,这和以前写的一维数组做杨辉三角很像!

感觉宋爹应该很快就能理解这个。。。

下面代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 250005;
int v[maxn],f[maxn];
int main(){
    int n,i,j;
    while(scanf("%d",&n)&&n>0){
        memset(v, 0, sizeof(v));
        memset(f, 0, sizeof(f));
        int number=1,sum=0;
        for(i=1;i<=n;i++){
            int t1,t2;
            scanf("%d%d",&t1,&t2);
            for(j=1;j<=t2;j++){
                sum += t1;
                v[number] = t1;
                number++;
            }
        }
        int half = sum/2;
        for(i=1;i<=number;i++){
            for(j=half;j>=v[i];j--){
                f[j] = max(f[j],f[j-v[i]]+v[i]);
            }
        }
        printf("%d %d\n",sum-f[half],f[half]);
    }
    return 0;
}

这题好像还能用母函数来做,哪天学会了补上吧


你可能感兴趣的:(01背包,待补完)