A Singing Contest
时间限制:11 秒 内存限制: 256M
Jigglypuff is holding a singing contest. There are 2 n singers indexed from 1 to 2 n 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 2 n is the number of singers.
(1 ≤ n ≤ 14)
Each of the next 2 n lines contains n integers where a ij is the pleasantness of the j-th song of the i-
th singer. It is guaranteed that all these 2 n x n integers are pairwise distinct. (1≤ a ij ≤ 10 9 )
输出描述:
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
比赛的时候建了个非递归线段树,从叶子一直模拟到顶点,可是一直MLE,然后就想用一维数组模拟,可是当时思路比较混乱,就做别的题去了。赛后看别人代码,发现用set模拟非常简明,自带二分,自带sort。然后dfs深搜,也比手动非递归线段树简单。
#include <bits/stdc++.h>
using namespace std;
const int N=(1<<14)+5;
set<int> sg[N];
int n,m;
int dfs(int l,int r){
if(l==r)
return l;
int mid=(l+r)/2;
int win1=dfs(l,mid),win2=dfs(mid+1,r);
int m1=*sg[win1].rbegin(),m2=*sg[win2].rbegin();
if(m1>m2){
sg[win1].erase(sg[win1].upper_bound(m2));
return win1;
}else{
sg[win2].erase(sg[win2].upper_bound(m1));
return win2;
}
}
int main(void) {
ios::sync_with_stdio(false);
int T, kase = 1; cin >> T;
while (T--) {
cin >> n; m = n << 1;
for(int i=1;i<=m;i++){
sg[i].clear();
for(int j=1;j<=n;j++){
int x;cin>>x;
sg[i].insert(x);
}
}
cout<<"Case #"<<kase++<<": "<<dfs(1,m)<<endl;
}
return 0;
}