HDU1171 一维01背包DP

Big Event in HDU


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




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
 



题意:50种东西重量从1~50,分别最多有100个,两个人平分(要差距最小),分别能分多少

思路:典型的01背包问题,但是原来用的二维的公式不能用了,会爆内存。要用一维的,dp[i]表示不大于i的重量能取得的最大值。这个问题,i最大就6万多。



#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
const int INF=0x7fffffff;
const int MAX_N=10000;

int T,N,W;
int weight[5009];
int dp[51*25*50+9];//挑选重量不大于j时的最大值
int total,half,num,a,b;
void solve(){
    memset(dp,0,sizeof(dp));
    for(int i=0;i=weight[i];j--){
            dp[j]=max(dp[j],dp[j-weight[i]]+weight[i]);
        }
    }
}
int main(){

    while(cin>>N&&N>=0){
        total=0;
        num=0;
        for(int i=1;i<=N;i++){
            scanf("%d%d",&a,&b);
            for(int j=0;j


你可能感兴趣的:(刷题)