全WA求调

P1168 中位数

asas111 @ 2023-07-28 18:49:22

样例过了,下载了数据发现输出一样

#include<bits/stdc++.h>
using namespace std;
int main(){
    priority_queue<int> p;
    priority_queue<int,vector<int>,greater<int> > q;
    int n,x,y,m;
    cin>>n>>x;
    p.push(x);
    for(int i=2;i<=n;i+=2){
        m=p.top();
        cout<<m<<endl;
        cin>>x>>y;
        if(x>y)swap(x,y);
        if(m<=x){
            q.push(x);
            q.push(y);
            p.push(q.top());
            q.pop();
        }
        else if(y<=m){
            p.push(x);
            p.push(y);
            q.push(p.top());
            p.pop();
        }
        else{
            p.push(x);
            q.push(y);
        }
    }
    cout<<p.top();
    return 0;
}

by changbijie_1019 @ 2023-08-01 18:57:29

注意n的奇偶性,加上这句就可以AC了:

if(n%2==0) n--;

by asas111 @ 2023-08-04 09:34:54

@changbijie1019 thx


|