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) != EOF && n != -1 ) {
        sum = 0;
        for ( int i = 1 ; i <= n ; ++ i ) {
            scanf("%d",&h[ i ]);
            sum += h[ i ];
        }

        memset( f, 0, sizeof( f ) );
        f[ 0 ][ 0 ] = 1;
        for ( int i = 1 ; i <= n ; ++ i )
        for ( int l = n/2+1 ; l > 0 ; -- l )
        for ( int j = sum/2 ; j >= h[ i ] ; -- j )
            if ( f[ l-1 ][ j-h[ i ] ] )
                f[ l ][ j ] = 1;

        int move = sum/2;
        while ( move ) {
            if ( n%2 == 0 && f[ n/2+0 ][ move ] ) break;
            if ( n%2 == 1 && f[ n/2+1 ][ move ] ) break;
            if ( n%2 == 1 && f[ n/2-0 ][ move ] ) break;
            -- move;
        }

        printf("%d %d\n",move,sum-move);
    }
    return 0;
}
时间: 2024-11-05 18:46:07

zoj 1880 - Tug of War(DP)的相关文章

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

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

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

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

ZOJ 3211 Dream City (J) DP

Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins

ZOJ 3551 Bloodsucker (概率DP)

ZOJ Problem Set - 3551 Bloodsucker Time Limit: 2 Seconds      Memory Limit: 65536 KB In 0th day, there are n-1 people and 1 bloodsucker. Every day, two and only two of them meet. Nothing will happen if they are of the same species, that is, a people

zoj 3791 An Easy Game dp

An Easy Game Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Edward and Flandre play a game. Flandre will show two 01-strings s1 and s2, the lengths of two strings are n. Then, Edward must move exact k steps. In each step, Edward should ch

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