link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=503
分成2半,并且两半的差距最小,背包的体积变成V/2
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <queue> 9 #include <deque> 10 #include <queue> 11 #include <list> 12 #include <map> 13 #include <set> 14 #include <vector> 15 #include <utility> 16 #include <functional> 17 #include <fstream> 18 #include <iomanip> 19 #include <sstream> 20 #include <numeric> 21 #include <cassert> 22 #include <ctime> 23 #include <iterator> 24 const int INF = 0x3f3f3f3f; 25 const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; 26 using namespace std; 27 int V, c[111], w[111], f[55555], n; 28 int main(void) 29 { 30 #ifndef ONLINE_JUDGE 31 freopen("in.txt", "r", stdin); 32 #endif // ONLINE_JUDGE 33 int t; cin>>t; 34 while (t--) 35 { 36 cin>>n; int sum=0; 37 for (int i = 0; i < n; ++i) 38 { 39 cin>>c[i]; sum+=c[i]; 40 } 41 V=sum/2; 42 memset(f, 0, sizeof(f)); 43 for (int i = 0; i < n; ++i) 44 { 45 for (int v = V; v>=c[i]; --v) 46 { 47 f[v] = max(f[v], f[v-c[i]]+c[i]); 48 } 49 } 50 cout<<fabs(f[V]-(sum-f[V]))<<endl; 51 } 52 return 0; 53 }
这题就用codeblocks写的,写的好纠结,不习惯,囧