很棒的DP,不过没想出,看题解了。。思维很重要。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 int dp[1000010]; 9 int sum[51]; 10 class MayTheBestPetWin 11 { 12 public : 13 int calc(vector <int> A, vector <int> B) 14 { 15 int i,j,s1,s2; 16 s1 = s2 = 0; 17 memset(dp,-1,sizeof(dp)); 18 dp[0] = 0; 19 for(i = 0;i < A.size();i ++) 20 { 21 sum[i] = A[i] + B[i]; 22 } 23 for(i = 0;i < A.size();i ++) 24 { 25 s1 += A[i]; 26 s2 += B[i]; 27 for(j = 1000000;j >= sum[i];j --) 28 { 29 if(dp[j-sum[i]] != -1&&dp[j] <= dp[j-sum[i]] + sum[i]) 30 { 31 dp[j] = dp[j-sum[i]] + sum[i]; 32 } 33 } 34 35 } 36 int minz = 10000000,t1,t2; 37 for(i = 1;i <= 1000000;i ++) 38 { 39 if(dp[i] != -1) 40 { 41 t1 = abs(s1-i); 42 t2 = abs(s2-i); 43 minz = min(minz,max(t1,t2)); 44 } 45 } 46 return minz; 47 } 48 };