2018牛客多校训练----Singing Contest

链接: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.

示例1

输入

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

输出

Case #1: 2
Case #2: 4

  共2的n次方的选手,每个选手准备了n首歌,每一首歌有对应的分数,每首歌的分数互不相同。相邻的选手两两比较,演唱一首歌的分数高的选手晋级下一轮,最终获胜的选手是谁。每个选手都尽最大努力赢。

 思路:

  模拟一下比赛的淘汰过程。

#include<bits/stdc++.h>
using namespace std;
 
const int N=1e5+100;
 
multiset<int>s[N];
multiset<int>::iterator it1,it2;
int a[N];
 
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,i,j;
        scanf("%d",&n);
        //m=(1<<n);
        m=pow(2,n);
        for(i=1;i<=m;i++)
        {
            a[i]=i;
            s[i].clear();
            for(j=1;j<=n;j++)
            {
                int x;
                scanf("%d",&x);
                s[i].insert(x);
            }
        }
        for(i=1;i<=n;i++,m/=2)
        {
            for(j=1;j<=m;j+=2)
            {
                it1=s[a[j]].end();
                it2=s[a[j+1]].end();
                it1--,it2--;
                if(*it1>*it2)
                {
                    s[a[j]].erase(s[a[j]].lower_bound(*it2));
                    a[(j+1)/2]=a[j];
                }
                else
                {
                    s[a[j+1]].erase(s[a[j+1]].lower_bound(*it1));
                    a[(j+1)/2]=a[j+1];
                }
            }
        }
        printf("Case #%d: %d\n",cas++,a[1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinghui_7/article/details/81434258