牛客网暑期ACM多校训练营(第六场)【线段树】

链接: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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值