zoukankan      html  css  js  c++  java
  • HDU-2576 Tug of War

    http://poj.org/problem?id=2576

    二维数组01背包的变形。

    Tug of War
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 8147   Accepted: 2191

    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
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std; 
    int weight[120],sum,sum1,dp[50000][103];
    int Max(int x,int y)
    {
        if(x>y)
        return x;
        else
        return y;
    }
    int main()
    {
       
        int n,i,n1,j,k;
          while(~scanf("%d",&n))
          {
           memset(dp,0,sizeof(dp));
            sum=0;
          for(i=1;i<=n;i++)
            {
             cin>>weight[i];
             sum+=weight[i];
            }
            dp[0][0]=1;
           sum1=sum/2;
           n1=(n+1)/2;
          for(i=1;i<=n;i++)
           for(j=sum;j>=weight[i];j--)
             for(k=n1;k>0;k--)
                 if(dp[j-weight[i]][k-1])
                      dp[j][k]=1;
            for(i=sum1;i>=0;i--)
             if(dp[i][n1]||dp[i][n1-1])
                  break;
            cout<<i<<" "<<sum-i<<endl;
    
          }
          return 0;
    }
    
  • 相关阅读:
    python线程的几种创建方式
    python进程之间的通信——Queue
    python中进程的几种创建方式
    python中的生成器、迭代器、闭包、装饰器
    java中String和StringBuffer的区别
    python中的元类介绍
    宣传片制作技巧
    树莓派创建WiFi热点
    PCB布线要求
    jsoncpp linux平台编译和arm移植
  • 原文地址:https://www.cnblogs.com/cancangood/p/3590744.html
Copyright © 2011-2022 走看看