链接:https://www.nowcoder.com/acm/contest/144/A
来源:牛客网
题目描述
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.
输入
2 1 1 2 2 1 8 2 7 3 4 5 6
输出
Case #1: 2 Case #2: 4
题意:晋级赛,两两比拼,从储备曲库自选一首歌进行比拼,每首歌只能用一次
思路:贪心,两两捉对,拿出两人最好的歌曲进行比对,胜利的一方拿出自己比对面多的最小花费歌曲,这样能保存最大实力
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct node
{
int id;
int data[15];
};
int main()
{
int t;
int n;
node u,v;
int k;
scanf("%d",&t);
for(int cases=1;cases<=t;cases++)
{
queue<node>sing;
scanf("%d",&n);
node x;
for (int i = 1; i <= 1<<n ; ++i)
{
for (int j = 0; j <n ; ++j)
scanf("%d",&x.data[j]);
x.id=i;
sort(x.data,x.data+n);
sing.push(x);
}
while (!sing.empty())
{
if(sing.size()==1) break;
u=sing.front();
sing.pop();
v=sing.front();
sing.pop();
if(u.data[n-1]>v.data[n-1])
{
k=upper_bound(u.data,u.data+n,v.data[n-1])-u.data;
u.data[k]=0;
sort(u.data,u.data+n);
sing.push(u);
}
else
{
k=upper_bound(v.data,v.data+n,u.data[n-1])-v.data;
v.data[k]=0;
sort(v.data,v.data+n);
sing.push(v);
}
}
node ans=sing.front();
printf("Case #%d: %d\n",cases,ans.id);
}
return 0;
}