P4715 【深基16.例1】淘汰赛

P4715 【深基16.例1】淘汰赛 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

就是将二叉树的叶子节点给出了,让我们模拟找到根

顺序表:

数组做法:

#include <iostream>
using namespace std;

int n, a[150], id[150]; //a存能力,id存编号
int main()
{
    cin >> n;
    
    int t = 1;
    while(n -- )
    {
        t *= 2;
    }
    n = t;
    for(int i = 1; i <= n; i ++ )
    {
        id[i] = i;
        cin >> a[i];
    }
    
    while(n != 1)
    {
        // for(int i = 1; i <= n; i ++ ) cout << a[i] << ' ';
        // cout << endl;
        
        int cnt = 1;
        if(n == 2)
        {
            if(a[1] > a[2])
            {
                cout << id[2] << endl;
            }
            else 
            {
                cout << id[1] << endl;
            }
            break;
        }
        for(int i = 1; i <= n; i += 2)
        {
            if(a[i] > a[i + 1])
            {
                a[cnt] = a[i];
                id[cnt ++] = id[i];
            }
            else
            {
                a[cnt] = a[i + 1];
                id[cnt ++] = id[i + 1];
            }
        }
        
        n /= 2;
    }
    
    return 0;
}

队列做法:就是每次取两个比较,然后再放在队尾

#include <iostream>
#include <queue>
using namespace std;

int n;
queue<pair<int, int>> q;

int main()
{
    cin >> n;
    n = 1 << n; //2^n
    
    for(int i = 1; i <= n; i ++ )
    {
        int x;
        cin >> x;
        
        q.push({i, x});
    }
    
    // cout << q.size() << endl;
    while(q.size() != 2)
    {
        auto a = q.front();
        q.pop();
        auto b = q.front();
        q.pop();
        
        if(a.second > b.second ) 
        {
            q.push(a);
        }
        else
        {
            q.push(b);
        }
        
        if(q.size() == 2)
        {
            
        }
    }
    
    auto a = q.front();
    q.pop();
    auto b = q.front();
    q.pop();
    
    if(a.second > b.second ) 
    {
        cout << b.first << endl;
    }
    else
    {
        cout << a.first << endl;
    }
    
    return 0;    
}

对顶栈写法:可惜re了

#include <iostream>
#include <stack>
using namespace std;

int n;
stack<pair<int, int>> q, tmp;

int main()
{
    cin >> n;
    n = 1 << n; //2^n
    
    for(int i = 1; i <= n; i ++ )
    {
        int x;
        cin >> x;
        
        q.push({i, x});
    }
    
    // cout << q.size() << endl;
    while(tmp.size() != 2 && q.size() != 2)
    {
        while(q.size())
        {
            auto a = q.top();
            q.pop();
            auto b = q.top();
            q.pop();
            if(a.second > b.second)
            {
                tmp.push(a);
            }
            else
            {
                tmp.push(b);
            }
        }
        
        while(tmp.size())
        {
            auto a = tmp.top();
            tmp.pop();
            auto b = tmp.top();
            tmp.pop();
            if(a.second > b.second)
            {
                q.push(a);
            }
            else
            {
                q.push(b);
            }
        }
        
    }
    if(tmp.size() == 2) 
    {
        auto a = tmp.top();
        tmp.pop();
        auto b = tmp.top();
        tmp.pop();
        if(a.second > b.second)
        {
            cout << b.first << endl;
        }
        else
        {
            cout << a.first << endl;
        }
    }
     if(q.size() == 2) 
    {
        auto a = q.top();
        q.pop();
        auto b = q.top();
        q.pop();
        if(a.second > b.second)
        {
            cout << b.first << endl;
        }
        else
        {
            cout << a.first << endl;
        }
    }
    return 0;    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值