Singing_Contest_( 结构体_数组 )

题目链接

题目描述

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场比赛,比赛只是相邻的进行比赛。

如图所示,然后每位选手有N首歌,但是每个选手只能唱一遍。

所以模拟即可,用到了结构体里套数组(骚),还有,用队列来模拟这个整个过程。

#include<bits/stdc++.h>
using namespace std;
const int N=2e4+10;
typedef struct node{
   // node (int m=-1):maxz(m){};
    int maxz,song[20],No;
}node;
int t=1,n;
void solve(){

    scanf("%d",&n);
    int round=n;
    node a[N];
    queue<int>q;
    for(int i=1;i<=(1<<n);i++){
        a[i].No=i;
        a[i].maxz=0;
        for(int j=0;j<n;j++){
            scanf("%d",&a[i].song[j]);
            a[i].maxz=max(a[i].maxz,a[i].song[j]);
        }
        q.push(i);
        sort(a[i].song,a[i].song+n);
    }

    /*for(int i=1;i<=(1<<n);i++){
        printf("第%d个: max: %d  ",i,a[i].maxz);
        for(int j=0;j<n;j++){
            printf("%d%c",a[i].song[j],j==n-1?'\n':' ');
        }
    }*/
    int ans=-1;
    int tot=0;
    while(!q.empty()){
        tot++;
        int t1=q.front();q.pop();
        if(q.empty()){
            ans=a[t1].No;break;
        }
        int t2=q.front();q.pop();
        if(a[t1].maxz>a[t2].maxz){
            int tmp=lower_bound(a[t1].song,a[t1].song+n,a[t2].maxz)-a[t1].song;
            a[t1].song[tmp]=0;
            sort(a[t1].song,a[t1].song+n);
            a[t1].maxz=a[t1].song[n-1];
            //printf("第%d轮 %d,%d\n",tot,t1.No,t1.maxz);
            q.push(t1);
        }else{
            int tmp=lower_bound(a[t2].song,a[t2].song+n,a[t1].maxz)-a[t2].song;
            a[t2].song[tmp]=0;
            sort(a[t2].song,a[t2].song+n);
            a[t2].maxz=a[t2].song[n-1];
            //printf("第%d轮 %d,%d\n",tot,t2.No,t2.maxz);
            q.push(t2);
        }
    }
    printf("Case #%d: %d\n",t,ans);
}
int main()
{
    int T;
    for(scanf("%d",&T);t<=T;t++){
        solve();
    }
    return 0;
}
/*
1000
3
7 1 6
2 5 9
11 10 3
19 8 4
2 9 16
15 10 11
3 5 7
6 4 2

2
1 8
2 7
3 4
5 6
*/

相似的还有一题:

https://blog.csdn.net/Z_sea/article/details/79146488

猜你喜欢

转载自blog.csdn.net/Z_sea/article/details/81475961