动态规划题:dp[i][j]表示有i个Cake,给了Alice j个,先按照b排序,这样的话,能保证每次都能成功给Alice Cake,因为b从大到小排序,所以Alice选了j个之后,Bob最少选了j个,所以i>=2*j, 并且每次Alice选的时候Bob已经选过了。所以当i>=2 * j的时候Alice一定能选. 所以dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + ary[i].a);
dp[i - 1][j]表示Alice不选第i个,dp[i - 1][j - 1] + ary[i].a表示Alice选第i个。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 888; int dp[maxn][maxn]; struct Node { int a, b; bool operator < (const Node &d) const { return b > d.b; } }ary[maxn]; int main() { int T, n; scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d %d", &ary[i].a, &ary[i].b); sort(ary + 1, ary + n + 1); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= (i / 2); j++) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + ary[i].a); } } printf("%d ", dp[n][n / 2]); } return 0; }