P4715 【深基16.例1】淘汰赛(2种方法)

题目:P4715 【深基16.例1】淘汰赛
输入:
3
4 2 3 1 10 5 9 7
输出:
1

#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct node {
    int id, x;
}a[100000 + 10];
bool cmp(node x, node y) {
    return x.x < y.x;
}
int main()
{
    int n;
    cin >> n;
    int x = pow(2, n);
    for (int i = 1; i <= x; i++)
    {
        cin >> a[i].x;
        a[i].id = i;
    }
    //把队伍分成两组,前一半一组,后一半一组
    sort(a+1, a + (x / 2)+1, cmp);//前一半排序
    sort(a + (x / 2)+2, a + x+1, cmp);//后一半排序
    int x1 = a[x / 2].x;
    int y = a[x].x;
    //cout << x1 << " " << y << endl;
    if (x1 < y) {//两组的分高的pk, 输出输的编号
        cout << a[x/2].id << endl;
    }
    else {
        cout << a[x].id << endl;
    }

    return 0;
}


树状数组做法:(想复习一下数状数组,就顺手写了,有点菜,(●’◡’●))
在这里插入图片描述

#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxn = 100000 + 10;
int n;
struct node {
    int x, id;
}tree[maxn];//保存为结点
int lowbit(int k) {
    return k & -k;
}
void updata(int index, node num,int x) {
    while (index <=x) {
        if (tree[index].x < num.x) {
            tree[index].x = num.x;
            tree[index].id = num.id;
        }
        index += lowbit(index);
    }
}
int main() {
    //int n;
    cin >> n;
   int  x = pow(2, n);
    for (int i = 1; i <= x/2; i++) {//创建两棵树,维护大的值
        // int b;
        node  b;
        cin >> b.x;
        b.id = i;
        updata(i, b,x/2);
    }
    for (int i = x/2+1; i <= x ; i++) {
        // int b;
        node  b;
        cin >> b.x;
        b.id = i;
        updata(i, b, x);
    }
    int c = tree[x / 2].x;
    int y = tree[x].x;
    
   // 对比两棵树的头部,输出较小的那个
    if (c <y) {
        cout << tree[x / 2].id;
    }
    else {
        cout << tree[x].id;
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值