列表

详情


NC17437. 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.

示例1

输入:

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

输出:

Case #1: 2
Case #2: 4

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 530ms, 内存消耗: 13288K, 提交时间: 2018-08-06 19:07:27

#include<cstdio>
#include<set>
using namespace std;
#define maxn (1<<14)+5
int T,n,a[maxn];
set<int>s[maxn]; 
set<int>::iterator it1,it2;
int main()
{
	scanf("%d",&T);
	int Case=1;
	while(T--)
	{
		scanf("%d",&n);
		int N=(1<<n);
		for(int i=1;i<=N;i++)
		{
			a[i]=i;
			s[i].clear();
			for(int j=0;j<n;j++)
			{
				int temp;
				scanf("%d",&temp);
				s[i].insert(temp);
			}
		}
		for(int k=1;k<=n;k++,N/=2)
		{
			for(int i=1;i<=N;i+=2)
			{
				it1=s[a[i]].end();
				it2=s[a[i+1]].end();
				it1--,it2--;
				if(*it1<*it2)
				{
					s[a[i+1]].erase(s[a[i+1]].lower_bound(*it1));
					a[(i+1)/2]=a[i+1];
				}
				else
				{
					s[a[i]].erase(s[a[i]].lower_bound(*it2));
					a[(i+1)/2]=a[i];
				}
			}
		}
		printf("Case #%d: %d\n",Case++,a[1]);
	}
	return 0;
}

C++ 解法, 执行用时: 269ms, 内存消耗: 14328K, 提交时间: 2021-10-06 12:55:44

#include<bits/stdc++.h>
using namespace std;
const int def=1<<16;

multiset<int>st[def];
int pos[def];

int main()
{	int T,m,x;
	scanf("%d",&T);
	for(int _=1;_<=T;_++){
		scanf("%d",&m);
		int n=1<<m;
		for(int i=1;i<=n;i++){
			st[i].clear();
			for(int j=1;j<=m;j++){
				scanf("%d",&x);
				st[i].insert(x);
			}
		}
		for(int i=1;i<=n;i++)pos[i]=i;
		for(int i=n;i>=1;i>>=1){
			for(int j=1;j+1<=i;j+=2){
				int x=pos[j],y=pos[j+1];
				int mx=*st[x].rbegin(),my=*st[y].rbegin();
				if(mx<my){
					swap(x,y);
					swap(mx,my);
				}
				st[x].erase(st[x].upper_bound(my));
				pos[j/2+1]=x;
			}
		}
		printf("Case #%d: %d\n",_,pos[1]);
	}
	return 0;
}

Python3(3.5.2) 解法, 执行用时: 1273ms, 内存消耗: 18276K, 提交时间: 2018-08-05 15:55:35

for _ in range(int(input())):
    n = int(input())
    a, b = [], []
    for i in range(1 << n):
        a.append((sorted(map(int, input().split())), i))
    while len(a) > 1:
        for i in range(0, len(a), 2):
            x0, i0 = a[i]
            x1, i1 = a[i + 1]
            index = [i0, i1][x0[-1] < x1[-1]]
            x0, x1 = sorted([x0, x1], key=lambda a: a[-1])
            for j in range(len(x1)):
                if x1[j] > x0[-1]:
                    break
            b.append((x1[:j] + x1[j + 1:], index))
        a, b = b, []
    print('Case #%d: %d' %(_ + 1, a[0][1] + 1))

上一题