链接:https://www.nowcoder.com/acm/contest/144/A
来源:牛客网
Singing Contest
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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>
using namespace std;
#define lson rt << 1
#define rson rt << 1 | 1
int n;
const int MAX = 1e6 + 7;
struct node{
vector <int> v;
int id;
}a[MAX << 2];
int id[MAX << 2];
void pushup(int rt){
int l = -1, r = -1;
for(int i = 0; i < a[lson].v.size(); i++){
int len = a[rson].v.size();
if(a[lson].v[i] > a[rson].v[len - 1]){
l = i;
// cout << l << endl;
break;
}
}
for(int i = 0; i < a[rson].v.size(); i++){
int len = a[lson].v.size();
if(a[rson].v[i] > a[lson].v[len - 1]){
r = i;
// cout << r << endl;
break;
}
}
if((l < r && l != -1) || l != -1 && r == -1){
vector <int> :: iterator it = a[lson].v.begin() + l;
a[lson].v.erase(it);
a[rt] = a[lson];
// cout << "LL" << endl;
}
else{
vector <int> :: iterator it = a[rson].v.begin() + r;
a[rson].v.erase(it);
a[rt] = a[rson];
// cout << "RR" << endl;
}
}
void build(int rt, int l, int r){
if(l == r){
vector <int> V;
for(int i = 0, x; i < n; i++){
scanf("%d", &x);
V.push_back(x);
}
sort(V.begin(), V.end());
a[rt].v = V;
a[rt].id = l;
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
pushup(rt);
}
int main(){
int N;
int kase = 0;
for(scanf("%d", &N); N; N--){
scanf("%d", &n);
int m = pow(2, n);
build(1, 1, m);
// for(int i = 1; i <= 10; i++) cout << a[i].id << " ";
// cout << endl;
printf("Case #%d: ", ++kase);
printf("%d\n", a[1].id);
}
return 0;
}
//333
//3
//22 25 23
//20 19 24
//14 17 18
//16 15 13
//8 11 12
//7 9 10
//3 5 6
//1 2 4