这是一个谔谔的问题

P1168 中位数

闪电皮卡丘 @ 2020-04-21 12:14:41

本机运行没有问题,交上去CE?

#include <bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,less<int> >da_q;
priority_queue<int,vector<int>,greater<int> >xiao_q;
int main()
{
    int n;
    cin>>n;
    int tmp1;
    cin>>tmp1;
    cout<<tmp1<<endl;
    da_q.push(tmp1);
    for(int i=2;i<=n;i++)
    {
        int tmp;
        cin>>tmp; 
        if(tmp>da_q.top())
        {
            xiao_q.push(tmp);   
        }
        else
        {
            da_q.push(tmp);
        }
        while(abs(da_q.size()-xiao_q.size())>1)
        {
            if(da_q.size()>xiao_q.size())
            {
                xiao_q.push(da_q.top());
                da_q.pop();
            }
            else
            {
                da_q.push(xiao_q.top());
                xiao_q.pop();
            }
        }
        if(i%2==1)
        {
            int tmp1=da_q.size(),tmp2=xiao_q.size(),tmp3=da_q.top(),tmp4=xiao_q.top()
            cout<<( tmp1 > tmp2 ? tmp3 : tmp4)<<endl;   
        }
    }
    return 0;
}

by Thomas_Cat @ 2020-04-21 12:15:14

在机房


by 闪电皮卡丘 @ 2020-04-21 12:15:33

@Thomas_Cat 啥啥意思


by uk_liptys @ 2020-04-21 12:16:23

#include <bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,less<int> >da_q;
priority_queue<int,vector<int>,greater<int> >xiao_q;
int main()
{
    int n;
    cin>>n;
    int tmp1;
    cin>>tmp1;
    cout<<tmp1<<endl;
    da_q.push(tmp1);
    for(int i=2;i<=n;i++)
    {
        int tmp;
        cin>>tmp; 
        if(tmp>da_q.top())
        {
            xiao_q.push(tmp);   
        }
        else
        {
            da_q.push(tmp);
        }
        while(abs(int(da_q.size()-xiao_q.size()))>1)
        {
            if(da_q.size()>xiao_q.size())
            {
                xiao_q.push(da_q.top());
                da_q.pop();
            }
            else
            {
                da_q.push(xiao_q.top());
                xiao_q.pop();
            }
        }
        if(i%2==1)
        {
            int tmp1=da_q.size(),tmp2=xiao_q.size(),tmp3=da_q.top(),tmp4=xiao_q.top();
            cout<<( tmp1 > tmp2 ? tmp3 : tmp4)<<endl;   
        }
    }
    return 0;
}

by 闪电皮卡丘 @ 2020-04-21 12:17:08

@uk_liptys dalao改了哪里看不来QWQ


by 闪电皮卡丘 @ 2020-04-21 12:17:58

@uk_liptys 啊?这么神奇QAQ


by IceYukino @ 2020-04-21 12:18:54

@闪电皮卡丘 ?


by Thomas_Cat @ 2020-04-21 12:22:58

@闪电皮卡丘 你在机房敲代码


by hahaha1215 @ 2022-03-02 20:59:57

队列的size()函数返回值是unsigned\ int类型,而unsigned\ int 类型的范围是[0,2^{32}-1],两个unsigned\ int类型相减自然还是unsigned\ int类型,也就是说不会出现负数!!!所以拿两个队列的size值相减的时候要转化成int类型!

只要改成while((int)(da.size()-xiao.size())>0)就好了!


by xuekaiwen_emmm @ 2023-01-02 21:52:05

size返回unsigned,不能用abs


|