Lightoj1147【二进制表状态】

思路:
dp[ i ]表示价值i需要多少人。
多一个就是状态往右移1位.
然后这个dp会爆long long但是由于每次会 | ,好像没什么影响,具体没影响也不清楚。。。

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define lson num<<1, Left, Mid
#define rson num<<1|1, Mid+1, Right
#define mem(a, b) memset(a, b, sizeof(a))
typedef pair<int,int> PII;

const double pi = acos(-1.0);
const double eps = 1e-6;

const int Maxn = 100000 + 10;

int a[Maxn], n;
LL dp[Maxn];

bool judge(LL sta){
    LL x, y;
    x = (n+1)/2;
    y = n-x;
    if((sta>>x)&1) return true;
    if((sta>>y)&1) return true;
    return false;
}

int main(){
    int T, cas = 1;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        int sum = 0, m;
        for(int i=1;i<=n;i++){
            scanf("%d", &a[i]);
            sum += a[i];
        }
        m = sum / 2;
        memset(dp, 0, sizeof(dp));
        dp[0] = 1;
        for(int i=1;i<=n;i++)
            for(int j=m;j>=a[i];j--)
            {
                if(dp[j-a[i]]){
                    dp[j] = dp[j] | (dp[j-a[i]]<<1);
//                    printf("j%d:%lld\n", j, dp[j]);
                }
            }

        int ans = 0;
        for(int i=m;i>=1;i--)
            if(judge(dp[i])){
                ans = i;
                break;
            }
        printf("Case %d: %d %d\n", cas++, ans, sum-ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/keyboarderqq/article/details/78665269