1057 Stack (30分)

本文介绍了一种特殊的数据结构——栈,该栈除了基本的Push和Pop操作外,还支持PeekMedian操作,即返回当前栈中所有元素的中位数。通过使用两个multiset来维护数据,确保在进行插入和删除操作时能够高效地更新中位数。

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian -- return the median value of all the elements in the stack. With Nelements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N(≤10​5​​). Then N lines follow, each contains a command in one of the following 3 formats:

Push key
Pop
PeekMedian

where key is a positive integer no more than 10​5​​.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print Invalid instead.

Sample Input:

17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop

Sample Output:

Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
multiset<int> set_l; 
multiset<int> set_r; 
int out_mid(){
    multiset<int>::iterator it;
    if(set_l.size() < set_r.size()){
        it = set_r.begin();
        set_r.erase(it);
        set_l.insert(*it);
    }
    if(set_l.size() > set_r.size() + 1){
        it = set_l.end();
        --it;
        set_l.erase(it);
        set_r.insert(*it);
    }
    int mid = 0;
    if(!set_l.empty()){
        it = set_l.end();
        --it;
        mid = *it;
    }
    
    return mid; 
}
int main(){
    int n;
    int mid;
    cin >> n;
    stack<int> st;
    

    for(int i = 0; i < n; i++){
        string s;
        cin >> s;   
        if(s == "Push"){
            int data;
            scanf("%d", &data);
            st.push(data);
            if(set_l.empty()){
                set_l.insert(data);
            }else if(data <= mid)
            {
                set_l.insert(data);
            }else
            {
                set_r.insert(data);
            }
            mid = out_mid();

        }else
        {
            if(st.empty()){
                printf("Invalid\n");
            }else
            {
                if(s == "Pop"){
                    printf("%d\n", st.top());
                    
                    if(st.top() <= mid)
                    {
                        set_l.erase(set_l.find(st.top()));
                    }else
                    {
                        set_r.erase(set_r.find(st.top()));
                    }
                    st.pop();
                    mid = out_mid();
            
                }else
                {       
                    
                    printf("%d\n", mid);
                }
                
            }
            
        }
        

    }

    return 0;
}

 

【SCI级别】多策略改进鲸鱼优化算法(HHWOA)和鲸鱼优化算法(WOA)在CEC2017测试集函数F1-F30寻优对比内容概要:本文档主要介绍了一项关于多策略改进鲸鱼优化算法(HHWOA)与标准鲸鱼优化算法(WOA)在CEC2017测试集函数F1-F30上进行寻优性能对比的研究,属于智能优化算法领域的高水平科研工作。文中通过Matlab代码实现算法仿真,重点展示了HHWOA在收敛速度、寻优精度和稳定性方面的优势,体现了多策略改进的有效性。该研究适用于复杂优化问题求解,尤其在工程优化、参数辨识、机器学习超参数调优等领域具有应用潜力。; 适合人群:具备一定算法基础和Matlab编程能力的研究生、科研人员及从事智能优化算法开发与应用的工程技术人员,尤其适合致力于SCI论文写作与算法创新的研究者。; 使用场景及目标:①用于理解鲸鱼优化算法的基本原理及多策略改进思路(如种群初始化、非线性收敛因子、精英反向学习等);②为智能优化算法的性能测试与对比实验提供CEC2017标准测试平台的实现参考;③支撑学术研究中的算法创新与论文复现工作。; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点关注HHWOA的改进策略模块与WOA的差异,通过重复实验验证算法性能,并可将其思想迁移至其他优化算法的改进中,提升科研创新能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值