zoukankan      html  css  js  c++  java
  • Problem F: Tug of War UVA 10032 DP

    G - Tug of War
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
    Submit Status
    Appoint description: 

    Description

    Download as PDF
     

    Problem F: Tug of War


    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 input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

    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

    For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

    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

    1
    
    3
    100
    90
    200
    

    Sample Output

    190 200
    T和WA了那叫一个惨!
    /*
     * 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][450*100 + 10];
    int MIN[maxn];
    int MAX[maxn];
    bool cmp(int x ,int y)
    {
        return x > y;
    }
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int T;
        scanf("%d",&T);
        int first = 0;
        while(T--)
        {
            if(first)
                printf("
    ");
            first = 1;
            int n;
            scanf("%d",&n);
    
            int sum = 0;
            repf(i,1,n)
            {
                scanf("%d",&num[i]);
                sum += num[i];
            }  
    
            clr(dp);
    
            dp[0][0] = 1;
    
            sort(num+1,num+1+n,cmp);
    
            MAX[0] = 0;
            repf(i,1,n)
                MAX[i] = MAX[i-1] + num[i];
    
            sort(num+1,num+1+n);
    
            MIN[0] = 0;
            repf(i,1,n)
                MIN[i] = MIN[i-1] + num[i];
    
            int N = n/2; 
    
            repf(i,1,n)
            {
                int temp = min(N,i);
                repd(k,temp - 1,0 )
                    repd(j,MAX[k],MIN[k])
                    {
                        if(dp[k][j])
                            dp[k+1][num[i] + j] = 1;
                    }
            }
    
    
            int ans = INF;
            repd(i,MAX[N],0)
            {
                if(dp[N][i])
                {
                    ans = min( ans , abs( sum - i - i ) );
                }
            }
            int m = ( sum - ans ) / 2;
            printf("%d %d
    ",m,sum-m);
    
        }
        return 0;
    }

  • 相关阅读:
    选中实现jquery实现复选框checkbox全选(完善)
    对话框control在对话框picture control中利用opengl进行绘图
    nullnull数字对象的常用处理方法NSNumber
    androidclassListView的Item含有CheckBox时的处理
    绘图对话框基于MFC对话框的OpenGL三维图形开发
    组织学习【学习笔记】《卓有成效的管理者》 第三章 我能贡献什么
    集合元素并查集
    开源请求程序员的黄金时代
    客户端生成nginx webdav配置
    情况虚拟化实战虚拟化存储设计之三MultiPathing
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3371549.html
Copyright © 2011-2022 走看看