牛客网暑期ACM多校训练营(第六场)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

思路:

用结构体存储每个歌手各首歌的愉悦值。排序后依次加入队列,每次将队首的两个人出对列。用二人最大值的愉悦值比较输赢后利用二分查找修改胜者的愉悦值数组。并且将胜者加入队尾,直到队列只剩下最后一个元素。

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
struct node{
    int a[15];
    int id;
};

int main(){
    int _;
    scanf("%d",&_);
    int zz=1;
    while(_--){
        queue<node >qu;
        int n;
        scanf("%d",&n);
        node x;
        for (int k = 1; k <=1<<n ; ++k) {
            for (int i = 0; i <n ; ++i) {
                scanf("%d,",&x.a[i]);
            }
            x.id=k;
            sort(x.a,x.a+n);
            qu.push(x);
        }
        node u,v;
        while (!qu.empty()){
            if(qu.size()==1)
                break;
            u=qu.front();
            qu.pop();
            v=qu.front();
            qu.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);
                qu.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);
                qu.push(v);
            }
        }
        v=qu.front();
        printf("Case #%d: %d\n",zz++,v.id);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/81414771
今日推荐