牛客网暑期ACM多校训练营(第六场)A Singing Contest 队列 +简单模拟

链接:https://www.nowcoder.com/acm/contest/144/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.

示例1

输入

复制

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

输出

复制

Case #1: 2
Case #2: 4

题意:举行歌唱比赛。有2n名参赛者从1到2n参赛,在第一轮中,歌手1与歌手2竞争,歌手3与歌手4竞争,依此类推;在第二轮中,歌手1和歌手2的获胜者与歌手3和歌手4的获胜者竞争,依此类推。总共有n轮。

每位歌手在比赛前准备了n首歌曲。每首歌都有独特的分数。在每一轮中,歌手都应该在他准备的歌曲中唱一首歌。为了不让观众失望,一首歌不能多次演出。以更高的分数演唱这首歌的歌手获胜。

现在所有的歌手都知道所有其他人准备的歌曲的分数。每个人都希望赢得尽可能多的回合。假设歌手最佳选择他们的歌曲,Jigglypuff想知道哪位歌手将赢得比赛?

思路:先两两出队;用数组排序每两个人的歌曲分数,进行比较,谁输了就从队列中pop出去,晋级的从新进队push;

代码:

#include<bits/stdc++.h>

using namespace std;
int a[20000][20];
queue<int >p2;
int main()
{
  int t,poss,i,j,n,c;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin>>t;
  c=0;
  while(t--)
  {
    cin>>n;
    poss=1<<n;
    for(i=1; i<=poss; i++)
    {
      for(j=1; j<=n; j++)

        cin>>a[i][j];

      p2.push(i);
    }
    while(p2.size()!=1)
    {
      int x=p2.front();
      p2.pop();
      int y=p2.front();
      p2.pop();//两个歌手出列进行比赛
      sort(a[x]+1,a[x]+1+n);
      sort(a[y]+1,a[y]+1+n);//两个人的歌曲分数排序
      if(a[x][n]>a[y][n])
      {
        for(i=1; i<=n; i++)
        {
          if(a[x][i]>a[y][n]) break;//寻找恰好比对手高分的歌曲
        }
        p2.push(x);//成功晋级 入队
        a[x][i]=0;//该歌曲分数归0
      }
      else
      {
        for(i=1; i<=n; i++)
        {
          if(a[y][i]>a[x][n])
            break;
        }
        p2.push(y);
        a[y][i]=0;
      }
    }
    printf("Case #%d: %d\n",++c,p2.front());//最后剩下一个人
    p2.pop();
  }
  return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值