求助QAQ

P1168 中位数

A_Đark_Horcrux @ 2019-10-03 20:43:32

如题

#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int,vector<int> > a;
priority_queue<int,vector<int>,greater<int> > b;
int abs(int x) {return x>=0?x:~x+1;}
int n,x,i;
int main()
{
    scanf("%d %d",&n,&x); a.push(x); printf("%d\n",a.top());
    for(i=2;i<=n;i++)
    {
        scanf("%d",&x);
        x>a.top()?b.push(x):a.push(x);
        while(abs(int(a.size())-int(b.size()))>1)
            a.size()>b.size()?b.push(a.top()),a.pop():a.push(b.top()),b.pop();
        if(i%2) printf("%d\n",a.size()>b.size()?a.top():b.top());
    }
    return 0;
}

by 羽儇 @ 2019-10-03 20:44:51

abs那里错惹


by 羽儇 @ 2019-10-03 20:47:46

@Bilion_冰凌帅 abs里面+1什麽鬼


by A_Đark_Horcrux @ 2019-10-03 20:51:03

@羽儇 好像没什么问题啊XD

而且改成这样:

int abs(int x) {return x>=0?x:-x;}

还是WA啊XDD


by 羽儇 @ 2019-10-03 21:06:59

@Bilion_冰凌帅

令人眼花缭乱的双目运算符

记住,

a.size()>b.size()?b.push(a.top()),a.pop():a.push(b.top()),b.pop();

是错误的写法,用双目写时,不能加逗号

#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int,vector<int> > a;
priority_queue<int,vector<int>,greater<int> > b;
int abs(int x) {return x>=0?x:-x;}
int n,x,i;
int main()
{
    scanf("%d %d",&n,&x); a.push(x); printf("%d\n",a.top());
    for(i=2;i<=n;i++)
    {
        scanf("%d",&x);
        x>a.top()?b.push(x):a.push(x);
        while(abs(int(a.size())-int(b.size()))>1)
        {
            if(a.size()>b.size())b.push(a.top()),a.pop();
            else a.push(b.top()),b.pop();
        }
        if(i%2) printf("%d\n",a.size()>b.size()?a.top():b.top());
    }
    return 0;
}

by 羽儇 @ 2019-10-03 21:07:19

改成这样就AC惹qwq


|