P1168 中位数

链接:Miku


对顶堆做法


#include<iostream> 
#include<cstdio>
#include<algorithm>
#include<queue>

using namespace std;
 priority_queue <int,vector<int>,greater<int> > q1;//小顶 
 priority_queue <int,vector<int>,less<int> >q2;//大顶 
 int n,k;
 int x;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d",&k);
        if(i==1){
        q2.push(k);
        cout<<k<<endl;
        continue;
        }
        else{
            if(q2.top()>k)
            q2.push(k);
            else
            q1.push(k);
        }
        if(q2.size()>q1.size()&&q2.size()-q1.size()>1){//不先比较会有莫名其妙越界问题
            x=q2.top();
            q1.push(x);
            q2.pop();
        }
        if(q1.size()>q2.size()&&q1.size()-q2.size()>1){
            x=q1.top();
            q2.push(x);
            q1.pop();    
        }
        if(i%2){
            if(q2.size()>q1.size())
            cout<<q2.top()<<endl;
            else
            cout<<q1.top()<<endl;
        }
    }
    return 0;
}

Ac

 

 
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇