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];
 8 int n;
 9 ll dp[50000];
10
11 int main()
12 {
13     int t;
14     scanf("%d",&t);
15     while(t--)
16     {
17         scanf("%d",&n);
18         int sum=0;
19         for(int i=0; i<n; i++)
20         {
21             scanf("%d",&w[i]);
22             sum+=w[i];
23         }
24         memset(dp,0,sizeof(dp));
25         dp[0]=1;
26         for(int i=0; i<n; i++)
27         {
28             for(int j=sum; j>=w[i]; j--)
29             {
30                 dp[j] |= dp[j-w[i]]<<1;
31             }
32         }
33         int ans1=0,ans2=0x3f3f3f3f;
34         for(int i=0; i<=sum; i++)
35         {
36             for(int j=0; j<=(n+1)/2; j++)
37             {
38                 if(dp[i]&(1ll<<j) && abs(2*j-n)<=1)
39                 {
40                     if(abs(sum-2*i)<ans2-ans1)
41                     {
42                         ans2=max(sum-i,i);
43                         ans1=min(sum-i,i);
44                     }
45                 }
46             }
47         }
48         printf("%d %d\n",ans1,ans2);
49         if(t) printf("\n");
50     }
51     return 0;
52 }

时间: 2024-10-23 04:54:44

uva 10032 Problem F: Tug of War的相关文章

uva 11400 Problem F Lighting System Design

紫皮书题: 题意:让你设计照明系统,给你n种灯泡,每种灯泡有所需电压,电源,每个灯泡的费用,以及每个灯泡所需的数量.每种灯泡所需的电源都是不同的,其中电压大的灯泡可以替换电压小的灯泡,要求求出最小费用 题解:每种电压灯泡要么全换,要么全不换,因为只换部分还要加额外的电源费用,并且换了部分之后费用更少,不如全换 先把灯泡按照电压从小到大排序,这样进行dp时,后面的电压大的如果比电压小的更优的话就可以替换了 设dp[j]为前j个最优了,则dp[i] = min{dp[i],dp[j] + (s[i]

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

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

XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game

题目: Problem F. Matrix GameInput file: standard inputOutput file: standard inputTime limit: 1 secondMemory limit: 256 mebibytesAlice and Bob are playing the next game. Both have same matrix N × M filled with digits from 0 to 9.Alice cuts the matrix ve

Problem F: 合唱比赛开始了!

Problem F: 合唱比赛开始了! Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 201[Submit][Status][Web Board] Description 为迎接计算机科技文化节的到来,我院面向一年级学生举办了一场合唱比赛.邀请了若干位专家担任评委,并为每个参赛队评分.现在,请设计一个程序来展示这个比赛过程. 其中,类 Team描述了参赛队的信息,包括:专业名称.每个专家给该参赛队的评分以及最终得分等.类C

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

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

Problem F CodeForces 16E

Description n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability