链接: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 <bits/stdc++.h>
#define rep(i,a,n) for(int i = a; i<n;i++)
#define per(i,a,n) for(int i = n-1;i>=a;i--)
#define se second
#define fi first
const int maxn = 1e5+10;
using namespace std;
int a[20];
vector<pair<int,set<int>> > V;
set<int>::iterator it1,it2;
int main()
{
int t;
a[0]=1;
rep(i,1,15)
a[i]=a[i-1]*2;
int cpp=0;
scanf("%d",&t);
while(t--)
{
int que[maxn];
memset(que,0,sizeof(que));
int head=1,top=0;
int n;
scanf("%d",&n);
int p = a[n];
V.clear();
for(int i=0;i<p;i++)
{
set<int>S;
for(int j = 0;j<n;j++)
{
int x;
scanf("%d",&x);
S.insert(x);
}
V.push_back(make_pair(i,S));
}
int vis[100];
memset(vis,0,sizeof(vis));
int len = V.size();
int cot = p;
int prei=-1,prej=-1;
for(int i=1;i<p;i+=2)
{
it1 = V[i].se.end();it1--;
it2 = V[i-1].se.end();it2--;
if( *it1 > *it2 )
{
it1 = V[i].se.lower_bound(*it2);
int temp = *(it1);
it1++;
V[i].se.erase(temp);
que[++top]=i;
// cout<<i<<endl;
}
else
{
it2 = V[i-1].se.lower_bound(*it1);
int temp = (*it2);
it2++;
V[i-1].se.erase(temp);
que[++top]=i-1;
// cout<<i-1<<endl;
}
}
while(head<top)
{
it1 = V[que[head]].se.end();it1--;
prei =que[head++];
// cout<<prei<<" ";
it2 = V[que[head]].se.end();it2--;
prej =que[head++];
// cout<<" | "<<prej<<endl;
if( *it1 > *it2 )
{
it1 = V[prei].se.lower_bound(*it2);
int temp = *(it1);
// cout<<temp<<endl;
it1++;
V[prei].se.erase(temp);
que[++top]= prei ;
}
else
{
it2 = V[prej].se.lower_bound(*it1);
int temp = (*it2);
it2++;
V[prej].se.erase(temp);
que[++top]=prej;
}
}
// cout<<head<<"<- ->"<<top<<endl;
printf("Case #%d: %d\n",++cpp,V[que[top]].fi+1);
}
return 0;
}
/*
3
3
2 11 12
1 27 14
7 50 22
9 23 19
4 8 41
3 10 13
5 20 21
6 14 15
*/
赛后代码:
#include <iostream>
#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i = n; i>=a;i--)
#define SI(x) scanf("%d",&x)
#define pb push_back
const int maxn = 1e5+10;
typedef long long ll;
using namespace std;
vector<int> a[1<<15];
vector<int>::iterator it;
int main()
{
int t;
int n;
int cot = 0;
SI(t);
while(t--)
{
SI(n);
rep(i,1,1<<n)
a[i].clear();
rep(i,1,1<<n)
{
int x;
rep(j,1,n)
{
SI(x);
a[i].pb(x);
}
sort(a[i].begin(),a[i].end());
}
queue<int>Q;
rep(i,1,1<<n)
Q.push(i);
while(Q.size()>1)
{
int u = Q.front(); Q.pop();
int v = Q.front(); Q.pop();
if(a[u].back() < a[v].back() )
swap(u,v);
it = upper_bound(a[u].begin(),a[u].end(),a[v].back());
a[u].erase(it);
Q.push(u);
}
printf("Case #%d: %d\n",++cot,Q.front());
}
return 0;
}