有2^n个人打淘汰赛,每次对战需要从自己的曲库中选一首,问都在最优情况下谁会获得最后的胜利。
两个人对战的时候,在双方都最优的情况下,自然是最大的歌较大的人赢。但是赢的那个人不一定要用最大的歌来对战,他需要用的是大于对面最大值的最小的歌。这样把所有人扔进队列里,每次取出要对战的两个人按照这样的方法进行模拟即可。
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 15;
int t, n, k, tmp;
struct node
{
int w[maxn];
int id;
}e, u, v, now;
queue<node> q;
int main()
{
scanf("%d", &t);
int kase = 0;
while(t--)
{
scanf("%d", &n);
while(!q.empty()) q.pop();
for(int i = 1;i <= (1<<n);i++)
{
for(int j = 0;j < n;j++)
scanf("%d", &e.w[j]);
sort(e.w, e.w + n);
e.id = i;
q.push(e);
}
while(!q.empty())
{
if(q.size() == 1) break;
u = q.front(); q.pop();
v = q.front(); q.pop();
if(u.w[n-1] > v.w[n-1]) now = u, tmp = v.w[n-1];
else now = v, tmp = u.w[n-1];
k = upper_bound(now.w, now.w + n, tmp) - now.w;
now.w[k] = 0;
sort(now.w, now.w + n);
q.push(now);
}
int ans = q.front().id;
printf("Case #%d: %d\n", ++kase, ans);
}
return 0;
}