台州 OJ 2378 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.

输入

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.

输出

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.

给 n 个人和他们各自的重量,要求把这 n 个人分成两部分,使这两部分的重量差值最小, 并且这两部分的人数差要小于等于 1。

用 vector 存放每个重量可能的人数,记得用一个 vis 数组判重,否则会超时。

代码:

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

const int MAX = 450*100/2 + 5;

vector<int> dp[MAX];    //到达 i 重量可能有多少人
bool vis[MAX][105];
int w[105];
int n;

int main(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    int tot = 0;
    cin >> n;
    for(int i=1; i<=n; i++){
        cin >> w[i];
        tot += w[i];
    }

    int maxSum = 0;
    memset(vis, 0, sizeof(vis));
    dp[0].push_back(0);        // 0 重量需要 0 个人
    vis[0][0] = 1;
    for(int i=1; i<=n; i++){
        for(int j=min(maxSum, tot/2); j>=0; j--){
            for(int k=0; k<dp[j].size(); k++){
                if(vis[j+w[i]][dp[j][k]+1] == 0){
                    dp[j+w[i]].push_back(dp[j][k] + 1);
                    vis[j+w[i]][dp[j][k]+1] = 1;
                }
            }
        }
        maxSum += w[i];
    }

    int ans = -1;
    for(int i=tot/2; i>=0; i--){
        for(int j=0; j<dp[i].size(); j++){
            if(n % 2 == 1){
                if(dp[i][j] >= n/2 && dp[i][j] <= n/2+1){
                    ans = i;
                    break;
                }
            }else{
                if(dp[i][j] == n/2){
                    ans = i;
                    break;
                }
            }
        }
        if(ans != -1)
            break;
    }

    cout << ans << " " << tot-ans;

    return 0;
} 
时间: 2024-11-04 17:01:47

台州 OJ 2378 Tug of War的相关文章

UVA - 10032 Tug of War (二进制标记+01背包)

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

lightoj-1147 - Tug of War(状压dp)

1147 - Tug of War PDF (English) Statistics ForumTime Limit: 4 second(s) Memory Limit: 32 MBA 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 o

POJ 2576 Tug of War 随机算法

原题地址:http://poj.org/problem?id=2576 Tug of War Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8525   Accepted: 2320 Description A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divide

随机算法 poj 2576 Tug of War

Tug of War Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8187   Accepted: 2204 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 b

ZOJ - 1880 Tug of War

题意:求在两边人数不相差超过1个的情况下,实力尽量相等的情况 思路:从实力和的一半开始类背包操作 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 45010; const int MAXM = 110; int a[MAXM]; int dp[MAXN][MAXM]; int

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given numbers

台州 OJ 3847 Mowing the Lawn 线性DP 单调队列

描述 After winning the annual town competition for best lawn a year ago, Farmer John has grown lazy; he has not mowed the lawn since then and thus his lawn has become unruly. However, the competition is once again coming soon, and FJ would like to get

poj 2576 Tug of War

还是神奇的随机算法,,(看视频说这是爬山法??) 其实就是把序列随机分成两半(我太弱,只知道random_shuffle),然后再每个序列里rand一个位置,x,y然后比较是不是交换之后是更优的. 然后重复这个过程. 神奇.. 1 #include<cstdio> 2 #include<cstring> 3 #include<ctime> 4 #include<iostream> 5 #include<algorithm> 6 #define L

台州OJ 3709: Number Maze (数组越界不报RE,报WA坑爹)

http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3709 You are playing one game called "Number Maze". The map of an example is shown in the following figure. In the map, there are N*N+2 cells. When the game starts, you