uva562 Dividing coins 01背包

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[][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}};
using namespace std;
int V, c[], w[], f[], 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=;
for (int i = ; i < n; ++i)
{
cin>>c[i]; sum+=c[i];
}
V=sum/;
memset(f, , sizeof(f));
for (int i = ; 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 ;
}

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

上一篇:《吊打面试官》系列-Redis基础知识


下一篇:SAP S4 Finance6个支持企业实时财务管理的主要创新领域