Tug of War POJ 2576 DP(类似背包)

Tug of War
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8085   Accepted: 2174

Description

A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more than 1; the total weight of the people on each team should be as nearly equal as possible.

Input

The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.

Output

Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers differ, give the lesser first.

Sample Input

3
100
90
200

Sample Output

190 200

Source

/*
 * Author:  
 * Created Time:  2013/10/14 22:43:39
 * File Name: A.cpp
 * solve: A.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d
", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 300;

int sgn(const double &x) {  return (x > eps) - (x < -eps); }
int num[maxn];
int dp[100+1][450*100+10];//dp[i][j]代表能否用i个数组成j
int main() 
{
    //freopen("in.txt","r",stdin);


    int n;
    while(scanf("%d",&n)==1)
    {
        int sum = 0;
        repf(i,1,n)
        {
            scanf("%d",&num[i]);
            sum += num[i];
        }        
        clr(dp);

        dp[0][0] = 1;

        int N = (n+1)/2; 
        repf(i,1,n)
        {
            repd(j,sum/2,0)
                repd(k,N,1)
                {
                    if(j>=num[i])
                    {
                        if(dp[k - 1][j - num[i]] == 1)
                            dp[k][j] = 1;
                    }
                }
        }


        int ans;
        repd(i,sum/2,0)
        {
            if(n%2 == 0)
            {
                if(dp[n/2][i] == 1)
                {
                    ans = i;
                    break;
                }
            }else
            {
                if(dp[n/2][i] == 1 || dp[n/2 + 1][i] == 1)
                {
                    ans = i;
                    break;
                }
            }
        }

        cout<<min(ans,sum-ans)<<" "<<max(ans,sum - ans)<<endl;
    }
    return 0;
}
/*
 * Author:  
 * Created Time:  2013/10/14 22:43:39
 * File Name: A.cpp
 * solve: A.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d
", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 300;

int sgn(const double &x) {  return (x > eps) - (x < -eps); }
int num[maxn];
int dp[100+1][450*100+10];//dp[i][j]代表能否用i个数组成j
int main() 
{
    //freopen("in.txt","r",stdin);


    int n;
    while(scanf("%d",&n)==1)
    {
        int sum = 0;
        repf(i,1,n)
        {
            scanf("%d",&num[i]);
            sum += num[i];
        }        
        clr(dp);

        dp[0][0] = 1;

        int N = (n+1)/2; 
        repf(i,1,n)
        {
            repf(j,0,sum/2)
                repf(k,0,N)
                {
                    if(dp[k][j] == 1)
                    {
                        if(j+num[i] <= sum/2)
                        {
                            dp[k+1][j+num[i]] = 1;
                        }
                    }

                }
        }


        int ans;
        repd(i,sum/2,0)
        {
            if(n%2 == 0)
            {
                if(dp[n/2][i] == 1)
                {
                    ans = i;
                    break;
                }
            }else
            {
                if(dp[n/2][i] == 1 || dp[n/2 + 1][i] == 1)
                {
                    ans = i;
                    break;
                }
            }
        }

        cout<<min(ans,sum-ans)<<" "<<max(ans,sum - ans)<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3370471.html