一点小问题

P1168 中位数

xianyuatm @ 2024-03-28 21:05:04

两遍代码,区别只在38 31行,为什么一个AC一个全哇wa

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define s(n) scanf("%lld",&n)
priority_queue <ll,vector<ll>,less<ll> > l;//qian
priority_queue <ll,vector<ll>,greater<ll> > r;//hou
int main()
{
    ll a;
    s(a);
    ll q,w;
    s(q),s(w);
    l.push(min(q,w)),r.push(max(q,w));
    cout<<q<<endl;
    for(int i = 3;i <= a;i++)
    {
//  cout<<l.top()<<"  "<<r.top()<<endl; 
        ll e;
        s(e);
        if(e <= r.top())
        {
            l.push(e);
        }
        else
        {
            r.push(e);
//          cout<<e<<endl;

        }
//      cout<<l.size()<<" "<<r.size()<<"  "<<l.top()<<"  "<<r.top()<<endl;  
        if(r.size() > l.size() + 1)
        {
            l.push(r.top());
            r.pop();    
//          if(e == 6)  

        }
        else if(l.size()>r.size()+1)
        {

            r.push(l.top());
            l.pop();
        }
//      cout<<endl<<l.size()<<"  "<<r.size()<<"  "<<l.top()<<"   "<<r.top()<<endl;
        if(i % 2)
        {
            if(r.size() > l.size())
            {
//              cout<<r.top()<<endl;                
            }
            else
            {
//              cout<<l.top()<<endl;
            }
        }
    }
    return 0;
 } 
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define s(n) scanf("%lld",&n)
priority_queue <ll,vector<ll>,less<ll> > l;//qian
priority_queue <ll,vector<ll>,greater<ll> > r;//hou
int main()
{
    ll a;
    s(a);
    ll q,w;
    s(q),s(w);
    l.push(min(q,w)),r.push(max(q,w));
    cout<<q<<endl;
    for(int i = 3;i <= a;i++)
    {
//  cout<<l.top()<<"  "<<r.top()<<endl; 
        ll e;
        s(e);
        if(e <= r.top())
        {
            l.push(e);
        }
        else
        {
            r.push(e);
//          cout<<e<<endl;

        }
        cout<<l.size()<<" "<<r.size()<<"  "<<l.top()<<"  "<<r.top()<<endl;  
        if(r.size() - l.size() > 1)
        {
            l.push(r.top());
            r.pop();    
//          if(e == 6)  

        }
        else if(l.size()-r.size() > 1)
        {

            r.push(l.top());
            l.pop();
        }
//      cout<<endl<<l.size()<<"  "<<r.size()<<"  "<<l.top()<<"   "<<r.top()<<endl;
        if(i % 2)
        {
            if(r.size() > l.size())
            {
//              cout<<r.top()<<endl;                
            }
            else
            {
//              cout<<l.top()<<endl;
            }
        }
    }
    return 0;
 } 

|