UVA 12260Free Goodies(dp+贪心)

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 ≤ 1 000): 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 ≤ 1 000) 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 in- and output
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

Output
170 130
14 16
9 10

题目意思是有一堆糖果,P和J分别对每科糖果有自己的标价。P和J轮流取,P的策略是每次选取对自己价值最高的糖果 (Petra always selects the goodie that is most valuable to her.and picks the one that is least valuable to Jan.),并且如果有糖果对于P来说价值一样,那他就选对于J来说价值最小的。J的策略是使自己的糖果的总价值尽可能高的同时,让P的糖果价值也尽可能高。( consists of maximizing his own final value. if multiple choices lead to the same optimal result, he prefers Petra to have as much final value as possible.)问最后两人的糖果价值?

解析:
对于P来说,他每次选当前对自己价值最高的糖果,采用贪心算法,可以对糖果按vp递减(当vp相等时vj递增)排个序,那么对于这个排好序的糖果堆,如果没有J存在的话,当然是P按照排好的顺序依次选取了。P的糖果值为sum(vp)。
那现在有J存在,P本来还是想依次取糖果,但是J可能会把中间的一些糖果拿掉,但这并不妨碍P依次取糖果,只是遇到被J拿掉的糖果时取后面一颗就好。P的糖果值为[sum(vp)-∑vp(i) ] (i为被J拿掉的糖果
j假设J先选:J因为要使自己的糖果总值最高,采用DP的方法,用数组dp[i][j]表示在前i个糖果中选了j个获得的价值。(在前i颗糖果中,dp[i][j]里面j<=(i+1)/2 )用cost[i][j]表示在前i个糖果中选了j个,这j个糖果的vp之和。在dp[i][j]相等的情况下,要cost[i][j]越小越好。因为P的糖果值为[sum(vp)-∑vp(i) ] (i为被J拿掉的糖果 ==sum(vp)-cost[n][(n+1)/2]
前面说的是当J先选的情况,当P先选的话,那么第一颗糖果就是P的,剩下的糖果同前。

void solve() {
	int bo = 0;
	if (name[0] == 'P')
		bo = 1;
	memset(dp, 0, sizeof(dp));
	memset(cost, 0, sizeof(cost));
	for (int i = 1; i <= n - bo; i++) {
		for (int j = 1; j <= (i + 1)/2; j++) {
			if (dp[i - 1][j] > dp[i - 1][j - 1] + c[i + bo].j) {
				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] + c[i + bo].j) {
				dp[i][j] = dp[i - 1][j];
				cost[i][j] = min(cost[i - 1][j], cost[i - 1][j - 1] + c[i + bo].p);
			}
			else {
				dp[i][j] = dp[i - 1][j - 1] + c[i + bo].j;
				cost[i][j] = cost[i - 1][j - 1] + c[i + bo].p;
			}
		}
	}
	printf("%d %d\n", sum - cost[n - bo][(n - bo + 1) / 2], dp[n - bo][(n - bo + 1) / 2]);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值