CSU 1012: Prestige

13人阅读 评论(0)

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

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

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

#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
**********************************************************************/


个人资料
等级：
访问量： 112万+
积分： 2万+
排名： 413
博客专栏
 ACM入门专栏 文章：257篇 阅读：377332 程序员需要的逻辑思维 文章：286篇 阅读：511602 ACM中的数论 文章：56篇 阅读：78275 BAT等名企面试笔试题 文章：31篇 阅读：66494 python入门 文章：10篇 阅读：11557 Linux（内核）基础 文章：18篇 阅读：5689