Petra and Jan have just received a box full of free goodies, and want to divide the goodies between them. However, it is not easy to do this fairly, since they both value different goodies differently. To divide the goodies, they have decided upon the following procedure: they choose goodies one by one, in turn, until all the goodies are chosen. A coin is tossed to decide who gets to choose the first goodie. Petra and Jan have different strategies in deciding what to choose. When faced with a choice, Petra always selects the goodie that is most valuable to her. In case of a tie, she is very considerate and picks the one that is least valuable to Jan. (Since Petra and Jan are good friends, they know exactly how much value the other places on each goodie.) Jan’s strategy, however, consists of maximizing his own final value. He is also very considerate, so if multiple choices lead to the same optimal result, he prefers Petra to have as much final value as possible. You are given the result of the initial coin toss. After Jan and Petra have finished dividing all the goodies between themselves, what is the total value of the goodies each of them ends up with? Input On the first line a positive integer: the number of test cases, at most 100. After that per test case: • One line with an integer n (1 ≤ n ≤ 1000): the number of goodies. • One line with a string, either ‘Petra’ or ‘Jan’: the person that chooses first. • n lines with two integers pi and ji (0 ≤ pi , ji ≤ 1000) each: the values that Petra and Jan assign to the i-th goodie, respectively. Output Per test case: • One line with two integers: the value Petra gets and the value Jan gets. Both values must be according to their own valuations. Sample Input 3 4 Petra 100 80 70 80 50 80 30 50 4 Petra 10 1 1 10 6 6 4 4 7 Jan 4 1 3 1 2 1 1 1 1 2 1 3 1 4 Sample Output 170 130 14 16 9 10
两个人选东西,一个是贪心的策略,选当前对自己价值尽量大的,然后让对别人的价值尽量小
第二个人的策略是让自己最终的价值尽量大,同时让自己选的东西对别人的价值尽量小
对物品按第一个人的价值进行排序,(其实这就是第一个人对物品的选择序),然后在这个序列上进行动态规划。
DP[i][j]表示在前i个物品中选择j个第二个人获得的价值
cost[i][j[表示在前I个物品中选择j个第一个人损失的价值
dp[i][j] = dp[i-1][j-1] + a[i].val 注意j不能超过(i+1)/2
如果是贪心的先选就从序列中第二个元素开始DP
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<memory> #include<bitset> #include<string> #include<functional> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 1008 #define INF 0x3f3f3f3f struct node { int pi, ji; bool operator < (const node& rhs)const { if (pi == rhs.pi) return ji < rhs.ji; return pi > rhs.pi; } }; int dp[MAXN][MAXN], n, cost[MAXN][MAXN]; //dp[i][j] 表示前i个物品中选取j个 // cost[i][j]表示---的时候P损失的价值 node a[MAXN]; char op[10]; int main() { int T; scanf("%d", &T); while (T--) { memset(dp, 0, sizeof(dp)); memset(cost, 0, sizeof(cost)); int beg = 0, sum = 0; scanf("%d%s", &n, op); if (op[0] == 'P') beg = 1; for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].pi, &a[i].ji), sum += a[i].pi; sort(a + 1, a + n + 1); for (int i = 1; i <= n - beg; i++)//如果是P开始就dp处理n-1个物品,如果是j开始就dp n 个 { for (int j = 1; j <= (i + 1) / 2; j++) { if (dp[i - 1][j] > dp[i - 1][j - 1] + a[i + beg].ji) { dp[i][j] = dp[i - 1][j]; cost[i][j] = cost[i - 1][j]; } else if (dp[i - 1][j] == dp[i - 1][j - 1] + a[i + beg].ji) { dp[i][j] = dp[i - 1][j]; cost[i][j] = min(cost[i - 1][j], cost[i - 1][j - 1] + a[i + beg].pi); } else { dp[i][j] = dp[i - 1][j - 1] + a[i + beg].ji; cost[i][j] = cost[i - 1][j - 1] + a[i + beg].pi; } } } printf("%d %d ", sum - cost[n - beg][(n - beg + 1) / 2], dp[n - beg][(n - beg + 1) / 2]); } }