牛客网提高组round#1

1.

60pts

用堆来实现维护中位数。

用大跟堆维护小于中位数的数,用小跟堆维护比中位数大的数。

若两堆元素之差大于一,把中位数push进较少元素的堆,把较多元素堆的堆顶作为中位数,pop。

o(n^2log n)

#include<bits/stdc++.h>
using namespace std;
struct cmp1{
    bool operator ()(const long long a,const long long b) const{
    return a>b;
    }
};
struct cmp2{
    bool operator ()(const long long a,const long long b)const{
    return a<b;
    }
};
#define ll long long
priority_queue<long long,vector<long long>,cmp2>a;
priority_queue<long long,vector<long long>,cmp1>b;
ll x,az,n,len,aa[200300],maxer=0;
int main(){
 cin>>n>>len;
 for(int i=1;i<=n;i++)cin>>aa[i];
 for(int i=1;i<=(1+n-len);i++){
 x=aa[i];
 for(int j=i+1;j<=n;j++){
    az=aa[j];
    if(az>=x)b.push(az);
    else a.push(az);
    int s=a.size()-b.size();
 if((s>1)|(s<-1))
 if(a.size()>b.size()){
    b.push(x);
    x=a.top();
    a.pop();
    }  
    else{
    a.push(x);
    x=b.top();
    b.pop();
    }  
    if((j-i+1)>=len)maxer=max(maxer,x);
    }
    while(!a.empty())a.pop();
    while(!b.empty())b.pop();
}
   cout<<maxer;
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/lxzl/p/9624728.html