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


题意:

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


查看评论

Prestige牌电饭煲

Prestige是个品牌,在印度是个著名的家电品牌,中文意思是"威望"的意思. 目前发现, 厨房里用的锅, 电饭煲都是这个品牌. 质量还不错, 那个锅足足快有两寸厚,拿在手里沉甸甸的, 很是练臂力,倒...
  • scruffybear
  • scruffybear
  • 2009-06-09 02:37:00
  • 836

https的证书错误,错误码-1012问题及解决方案

经过半天的折腾终于解决了这个鸡肋的证书问题。 我们自己生成了一个ssl证书,使用https发送消息时会用到。 但是当把证书加入工程中,用AFNetworking发送请求时总是报-1012但是另一个人却...
  • jia12216
  • jia12216
  • 2015-10-17 16:22:18
  • 8489

[HeyJava][传智播客]BBS-day001-001.rar

  • 2010年07月14日 10:01
  • 9.55MB
  • 下载

PAT甲级1012

1012. The Best Rank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B ...
  • qq_22194315
  • qq_22194315
  • 2017-01-11 00:08:10
  • 353

CSU_1660_K-Cycle

1660: K-Cycle Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 124  Solved: 31 [Submit][Status][Web ...
  • baidu_29410909
  • baidu_29410909
  • 2015-08-09 17:34:49
  • 309

CSU 1100: 一二三【模拟】

1100: 一二三 Time Limit: 1 Sec  Memory Limit: 128 MB Description 你弟弟刚刚学会写英语的一(one)、二(two)和三(...
  • hurmishine
  • hurmishine
  • 2016-08-16 13:26:17
  • 1171

PAT_乙级1012

1012. 数字分类 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判...
  • Barbie_1229
  • Barbie_1229
  • 2017-07-31 17:48:09
  • 217

一二三 CSU - 1100

你弟弟刚刚学会写英语的一(one)、二(two)和三(three)。他在纸上写了好些一二三,可惜有些字母写错了。已知每个单词最多有一个字母写错了(单词长度肯定不会错),你能认出他写的啥吗?   ...
  • aftershowermeow
  • aftershowermeow
  • 2017-02-12 10:02:56
  • 117

浙大PAT 1012题 1012. The Best Rank

时间复杂度O(n^2),当n=2000时,复杂度为400 0000,本以为过不了。 但别人写的代码蛮简单的,400ms的时限,100ms能过。 #include #include #define ...
  • Sup_Heaven
  • Sup_Heaven
  • 2012-12-29 21:45:43
  • 2712

CSU/DSU是什么设备

channel service unit/data service unit 想象router是计算机,CSU/DSU是MODEM;就这样联。 ***CSU/DSU不能叫作MODEM***MODE...
  • Galdys
  • Galdys
  • 2011-08-04 16:20:47
  • 903
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 112万+
    积分: 2万+
    排名: 413
    博客专栏