牛客网暑期ACM多校训练营(第六场)A-Singing Contest

时间限制: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.

输入

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比赛等,然后晋级再进行比赛,每次只可以唱一首歌,根据歌曲的愉悦度打分,唱过的歌曲不能再唱,假设每个歌手尽可能赢得更多的轮数。请问最后的冠军是谁。

做法:模拟

利用队列的先进先出的性质。每次将俩个比赛选手弹出来进行比较。判断胜利方法为比较俩个选手歌曲的最大愉悦度,谁强谁赢。然后根据输的一方的歌曲最大愉悦度来寻找胜利方采取哪一首歌曲可以刚刚好胜利。将胜利方加入队列末尾。

代码如下:

#include<bits/stdc++.h>
using namespace std;
struct contestant
{
    int a[20];
    int id;
};
queue<contestant>q;
int  T;
int main(){
    scanf("%d",&T);
    int t=0;
    while(T--){
        while (!q.empty()) q.pop();
        t++;
        int n;
        scanf("%d",&n);
        contestant c;
        for(int i=1;i<=(1<<n);i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%d",&c.a[j]);
            }
            c.id=i;
            sort(c.a,c.a+n);
            q.push(c);
        }
       contestant s1,s2;
        while (true){
            if(q.size()==1)
                break;
            s1=q.front();
            q.pop();
            s2=q.front();
            q.pop();
            if(s1.a[n-1]>s2.a[n-1]){
                int k=n-1;
                for(int i=0;i<n;i++)
                {
                    if(s1.a[i]>s2.a[n-1])
                    {
                        k=i;
                        break;
                    }
                }
                s1.a[k]=0;把歌曲清为0
                sort(s1.a,s1.a+n);
                q.push(s1);
            }
            else{
                int k=n-1;
                for(int i=0;i<n;i++)
                {
                    if(s2.a[i]>s1.a[n-1])
                    {
                        k=i;
                        break;
                    }
                }
                s2.a[k]=0;//把歌曲清为0
                sort(s2.a,s2.a+n);
                q.push(s2);
            }
        }
        contestant ans=q.front();
        printf("Case #%d: %d\n",t,ans.id);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值