很棒的DP,不过没想出,看题解了。。思维很重要。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int dp[];
int sum[];
class MayTheBestPetWin
{
public :
int calc(vector <int> A, vector <int> B)
{
int i,j,s1,s2;
s1 = s2 = ;
memset(dp,-,sizeof(dp));
dp[] = ;
for(i = ;i < A.size();i ++)
{
sum[i] = A[i] + B[i];
}
for(i = ;i < A.size();i ++)
{
s1 += A[i];
s2 += B[i];
for(j = ;j >= sum[i];j --)
{
if(dp[j-sum[i]] != -&&dp[j] <= dp[j-sum[i]] + sum[i])
{
dp[j] = dp[j-sum[i]] + sum[i];
}
} }
int minz = ,t1,t2;
for(i = ;i <= ;i ++)
{
if(dp[i] != -)
{
t1 = abs(s1-i);
t2 = abs(s2-i);
minz = min(minz,max(t1,t2));
}
}
return minz;
}
};