CSU 1012: Prestige

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nameofcsdn/article/details/79952274

题目:

Description

Are you watching closely?

Every great magic trick consists of three acts. The first act is called The Pledge, the magician shows you something ordinary, but of course, it probably isn't. The second act is called The Turn. The magician makes his ordinary something do something extraordinary. Now, if you're looking for the secret,you won't find it. That's why there's a third act, called The Prestige. This is the part with the twists and turns, where lives hang in the balance, and you see something shocking you've never seen before.

Li Lei and Han Meimei are magicians and they want to act the prestige. So they need to divide the props for the magic show. They have decided upon the following procedure: they choose props one by one, in turn, until all the props are chosen.

Li Lei and Han Meimei have different strategies in deciding what to choose. When faced with a choice, Li Lei always selects the prop that is most valuable to him. In case of a tie, he is very considerate and picks the one that is least valuable to Han Meimei. (Since Li Lei and Han Meimei are good friends, they know exactly how much value the other places on each prop.)

Han Meimei’s strategy, however, consists of maximizing her own final value(The total value of all the props she picked). She is also very considerate, so if multiple choices lead to the same optimal result, she prefers Li Lei to have as much final value as possible.

You are given the result that who chooses first. After Li Lei and Han Meimei have finished dividing all the props between themselves, what is the total value of the props 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 props.
  • One line with a letter, either “L” for Li Lei or “H” for Han Meimei: the person that chooses first.
  • n lines with two integers li and hi (0≤li, hi≤1000) each: The values that Li Lei and Han Meimei assign to the i-th prop, respectively.

Output

For each test case, output one line with two integers: the value Li Lei gets and the value Han Meimei gets. Both values must be according to their own valuations.

Sample Input

3
4
L
100 80
70 80
50 80
30 50
4
L
10 1
1 10
6 6
4 4
7
H
4 1
3 1
2 1
1 1
1 2
1 3
1 4

Sample Output

170 130
14 16
9 10


题意:

有n个物品,L和H两人轮流来取

每个物品对L的价值为v1,对H的价值为v2

输入谁先取,和每个物品的价值v1v2,输出最后的结果

L的策略是每次都取v1最大的物品,如果有多个,就取其中v2最小的那个

H的策略是尽量使得最后所得总价值最大,如果有多个选择方案,就取其中v1最小的那个


思路:贪心

先把所有物品按照L的策略进行排序

然后H每次都取v2最大的物品,但是要满足限制,就是前面若干个物品中H最多只能取一半,因为L是一直按照这个顺序往后取的

只要满足这个限制,H就能得到最优解


代码:

#include<iostream>
#include<algorithm>
using namespace std;

int n;
char c;
bool visit[1001], ans[1001];

struct node
{
	int v1, v2;
}nod[1001];

bool cmp(node a, node b)
{
	if (a.v1 == b.v1)return a.v2 < b.v2;
	return a.v1 > b.v1;
}

int getmax()
{
	int max = 0, key = 0;
	for (int i = 1; i <= n; i++)
		if (!visit[i] && max <= nod[i].v2)max = nod[i].v2, key = i;
	return key;
}

bool ok(int k)
{
	int s = 0;
	for (int i = 1; i <= n; i++)
	{
		if (i == k || ans[i])s++;
		if (s > (i + (c == 'H'))/2)return false;
	}
	return true;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> c;
		for (int i = 1; i <= n; i++)
		{
			cin >> nod[i].v1 >> nod[i].v2;
			visit[i] = false, ans[i] = false;
		}
		sort(nod + 1, nod + n + 1, cmp);
		int num = (n + (c == 'H')) / 2;
		visit[1] = (c == 'L');
		while (num)
		{
			int k = getmax();
			if (ok(k))ans[k] = true, visit[k] = true, num--;
			else for (int i = 1; i <= k; i++)visit[k] = true;
		}
		int s1 = 0, s2 = 0;
		for (int i = 1; i <= n; i++)
			if (ans[i])s2 += nod[i].v2; else s1 += nod[i].v1;
		cout << s1 << " " << s2 << endl;
	}
	return 0;
}
/**********************************************************************
	Problem: 1012
	User: 3901140225
	Language: C++
	Result: AC
	Time:376 ms
	Memory:2032 kb
**********************************************************************/


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页