牛客网多校赛6 ASinging 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
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define MAX 1000000000
#define ms memset
using namespace std;
const int maxn=40000;

int tb[30];
int compute[maxn];
set<int> st[maxn/2];
/*
题目描述:有2^n个歌手,
每个人都准备了n首歌,
每个人都采取最好策略,
问最后谁赢。

模拟的思想是:两个人的歌曲集合,
博弈是看其最大值,最大值的那个获胜,然后最大值从集合中消去。
这样模拟下去。

这里要注意的是数据结构的使用,
set是一个自动排好序的集合,且支持上下界查找操作。

具体操作见代码,一些技巧而已,
比如取集合中的最大值,和删除上界等等。
*/

int judge(int x,int y)
{
    int maxx=*(--st[x].end()),maxy=*(--st[y].end());
    if(maxx>maxy)
    {
        st[x].erase(st[x].upper_bound(maxy));
        return 0;
    }
    else
    {
        st[y].erase(st[y].upper_bound(maxx));
        return 1;
    }
}

int n;
int main()
{
    tb[0]=1;for(int i=1;i<=30;i++) tb[i]=tb[i-1]*2;
    int t;scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        scanf("%d",&n);

        for(int i=1;i<=tb[n];i++) st[i].clear();
        memset(compute,0,sizeof(compute));

        int index=1;
        for(int i=tb[n];i<=tb[n+1]-1;i++)   compute[i]=index++;

        index--;
        for(int i=1;i<=index;i++)
        {
            int p;
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&p);
                st[i].insert(p);
            }
        }

        for(int i=tb[n+1]-1,j=i-1;j>0;i-=2,j-=2)
        {
            if(judge(compute[i],compute[j])==0)      compute[i/2]=compute[i];
            else   compute[i/2]=compute[j];
        }

        printf("Case #%d: %d\n",ca,compute[1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81416003
今日推荐