A Singing Contest(六)

链接:https://www.nowcoder.com/acm/contest/144/A
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Jigglypuff is holding a singing contest. There are 2n singers indexed from 1 to 2n participating in the contest.

The rule of this contest is like the knockout match. That is, in the first round, singer 1 competes with singer 2, singer 3 competes with singer 4 and so on; in the second round, the winner of singer 1 and singer 2 competes with the winner of singer 3 and singer 4 and so on. There are n rounds in total.

Each singer has prepared n songs before the contest. Each song has a unique pleasantness. In each round, a singer should sing a song among the songs he prepared. In order not to disappoint the audience, one song cannot be performed more than once. The singer who sings the song with higher pleasantness wins.

Now all the singers know the pleasantness of songs prepared by all the others. Everyone wants to win as many rounds as he can. Assuming that singers choose their song optimally, Jigglypuff wants to know which singer will win the contest?

输入描述:

The input starts with one line containing exactly one integer t which is the number of test cases. (1 ≤ t ≤ 10)

For each test case, the first line contains exactly one integer n where 2n is the number of singers. (1 ≤ n ≤ 14)

Each of the next 2n lines contains n integers where aij is the pleasantness of the j-th song of the i-th singer. It is guaranteed that all these 2nx n integers are pairwise distinct. (1 ≤ aij ≤ 109)

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x is the test case number (starting from 1) and y is the index of the winner.

示例1

输入

复制

2
1
1
2
2
1 8
2 7
3 4
5 6

输出

复制

Case #1: 2
Case #2: 4

题意:有2的n次方个人,每个人准备了n首歌,每个歌都有个魅力值,每一场比赛相邻的两个人进行比赛,1与2,3与4,

题解:首先定义一个结构体把每个人的编号与唱的每首歌的魅力值存入,利用队列的先进先出的原则,依次存入队列(魅力值从小到大排列),再一次比较魅力值大小,比较过的魅力值设为0,再进行一次排序,将获胜的再次存入队列,如此反复直到队列中只有一个为止。

代码:

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
	int a[15];
	int id;
};
int main()
{
	int t;
	while(scanf("%d",&t)!=EOF)
	{
		int q=0;
		while(t--){
			int n;scanf("%d",&n);
			q++;
			queue<node> p;
			for(int i=1;i<=(1<<n);i++)
			{
				node a;
				for(int j=0;j<n;j++)
				scanf("%d",&a.a[j]);
				a.id=i;
				sort(a.a,a.a+n);
				p.push(a);
			}
			node u,v;
			while(p.size()>1)
			{
				u=p.front();p.pop();
				v=p.front();p.pop();
				if(u.a[n-1]>v.a[n-1])
				{
					int k=upper_bound(u.a,u.a+n,v.a[n-1])-u.a;
					u.a[k]=0;
					sort(u.a,u.a+n);
					p.push(u); 
				}
				else
				{
					int k=upper_bound(v.a,v.a+n,u.a[n-1])-v.a;
					v.a[k]=0;
					sort(v.a,v.a+n);
					p.push(v); 
				}
			}
			u=p.front();
			printf("Case #%d: %d\n",q,u.id);
		}
	}
return 0;
} 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值