HDOJ-1171 Big Event in HDU

来源:互联网 发布:mac修改桌面图标大小 编辑:程序博客网 时间:2024/05/21 20:36

01背包解法求出最接近总价值一半的值即可

#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include <iostream>#include <sstream>using namespace std;struct Goods{    int v, m;}goods[55];int dp[130000];int main(){  //  freopen("in.txt", "r", stdin);    int n;    while(cin >> n && n >= 0)    {        int s = 0, p;        for(int i = 0; i < n ; i++)        {            cin >> goods[i].v >> goods[i].m;            s += goods[i].v * goods[i].m;        }        memset(dp, 0, sizeof(dp));        p = s;        s /= 2;        dp[0] = 1;        for(int i  = 0; i < n; i++)            for(int j = 0; j < goods[i].m; j++)            for(int h = s; h >= goods[i].v; h--)            dp[h] |= dp[h - goods[i].v];        for(int h = s; h >= 0; h--)            if(dp[h])        {            cout << p - h << " " << h << endl;            break;        }    }    return 0;}
0 0