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

int main(){
	int t,flag = 0;
	while (scanf("%d", &n) != EOF){
		int r = n/2;
		if (n & 1)
			r++;
		int sum = 0;
		for (int i = 0; i < n; i++){
			scanf("%d", &a[i]);
			sum += a[i];
		}
		memset(dp,0,sizeof(dp));
		dp[0][0] = 1;
		for (int i = 0; i < n; i++)
			for (int j = sum/2; j >= a[i]; j--)
				for (int k = r; k >= 1; k--)
					if (dp[j-a[i]][k-1])
						dp[j][k] = 1;
		int ans = 0;
		for (int i = sum/2; i >= 0; i--){
			if (n%2 == 0){
				if (dp[i][r]){
					ans = i;
					break;
				}
			}
			if (n%2 == 1){
				if (dp[i][r] || dp[i][r-1]){
					ans = i;
					break;
				}
			}
		}
		printf("%d %d\n", ans, sum-ans);
	}
	return 0;
}

ZOJ - 1880 Tug of War,布布扣,bubuko.com

时间: 2024-08-02 15:13:46

ZOJ - 1880 Tug of War的相关文章

zoj 1880 - Tug of War(DP)

题目:二维01背包. 分析:因为必须放在两个组中的一组,直接背包所有可到状态, 取出相差不超过 1的最接近 sum/2的值即可. 说明:430ms...好慢啊... #include <stdio.h> #include <stdlib.h> #include <string.h> int f[ 52 ][ 22501 ]; int h[ 101 ]; int main() { int n,sum; while ( scanf("%d",&n

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

台州 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 t

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

uva 10032 Problem F: Tug of War

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=973 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5 using namespace std; 6 7 int w[5000];

UVa 10032 - Tug of War

题目:有n个人分成两组,两组人数差不能超过1,找到两组的人重量之差的最小值. 分析:dp,状态压缩01背包.zoj1880升级版. 首先,考虑二维背包. 因为必须放在两个组中的一组,直接背包所有可到状态,取出相差不超过 1的最接近 sum/2的值即可. 然后,优化. 如果直接利用二维背包,由于数据组数较多会TLE,这里利用状态压缩的一维背包: 状态:f(i)表示总重为i时的所有可能的人数,f(i)的值,的第k位上的数是1则代表有k人的组合方式. 转移:f(i)= f(i)| (f(i-w[j])