uva562 Dividing coins 01背包

August 6, 2013
Uva DP

link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=503 分成2半,并且两半的差距最小,背包的体积变成V/2


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
int V, c[111], w[111], f[55555], n;
int main(void)
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int t; cin>>t;
    while (t--)
    {
        cin>>n; int sum=0;
        for (int i = 0; i < n; ++i)
        {
            cin>>c[i]; sum+=c[i];
        }
        V=sum/2;
        memset(f, 0, sizeof(f));
        for (int i = 0; i < n; ++i)
        {
            for (int v = V; v>=c[i]; --v)
            {
                f[v] = max(f[v], f[v-c[i]]+c[i]);
            }
        }
        cout<<fabs(f[V]-(sum-f[V]))<<endl;
    }
    return 0;
}

这题就用codeblocks写的,写的好纠结,不习惯,囧

comments powered by Disqus