ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#213997 | #2380. 中位数 | White_Wat | 30 | 26ms | 1456kb | C++11 | 2.1kb | 2024-11-14 22:00:23 | 2024-11-14 23:08:52 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+5;
int n;
int a[N];
vector<int> all;
//multiset<int> st;
mt19937 rnd(210828);
struct Fhq_Treap{
int v[N],_;
int siz[N],key[N],rt;
int ch[N][2];
void clear(){
rt=0,_=0;
}
int newnode(int x){
v[++_]=x,key[_]=rnd(),siz[_]=1,ch[_][0]=ch[_][1]=0;return _;
}
void pushup(int x){siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;}
void split_v(int now,int val,int &x,int &y){
if(!now) x=y=0;
else{
if(v[now]<=val){
x=now;
split_v(ch[x][1],val,ch[x][1],y);
}
else{
y=now;
split_v(ch[y][0],val,x,ch[y][0]);
}
pushup(now);
}
}
void split_s(int now,int s,int &x,int &y){
if(!now) x=y=0;
else{
if(siz[ch[now][0]]+1<=s){
x=now;
split_s(ch[now][1],s-(siz[ch[now][0]]+1),ch[now][1],y);
}
else{
y=now;
split_s(ch[now][0],s,x,ch[now][0]);
}
pushup(now);
}
}
int merge(int x,int y){
if(!x||!y) return x|y;
if(key[x]<key[y]){
ch[x][1]=merge(ch[x][1],y);
pushup(x);
return x;
}
ch[y][0]=merge(x,ch[y][0]);
pushup(y);
return y;
}
int x,y,z;
void insert(int val){
split_v(rt,val,x,y);
rt=merge(merge(x,newnode(val)),y);
}
int mid(){
int sz=siz[rt];
// cout<<sz<<':';
int k=(sz+1)/2;
split_s(rt,k-1,x,y);
split_s(y,1,y,z);
// cout<<siz[y]<<'?';
int re=v[y];
rt=merge(merge(x,y),z);
return re;
}
void ldr(int x){
if(ch[x][0]) ldr(ch[x][0]);
cout<<v[x]<<' ';
if(ch[x][1]) ldr(ch[x][1]);
}
}st;
int main(){
// freopen("ex_median2.in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++){
st.clear();
for(int j=i;j<=n;j++){
st.insert(a[j]);
// cout<<i<<' '<<j<<' '<<st.mid()<<' ';
// st.ldr(st.rt);cout<<'\n';
all.push_back(st.mid());
}
}
// for(auto i:all)
// cout<<i<<' ';
sort(all.begin(),all.end());
if(all.size()%2==1)
cout<<all[all.size()/2+1];
else
cout<<all[(all.size()+1)/2];
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 10ms
memory: 1452kb
input:
200 247837946 222748444 549404903 934970141 181690491 116150147 755881953 912914845 578004877 589768...
output:
411892743
result:
ok 1 number(s): "411892743"
Test #2:
score: 10
Accepted
time: 6ms
memory: 1452kb
input:
200 504553675 321343511 781936447 172776833 779139954 643752948 106 164 303976944 828799931 11502170...
output:
451369372
result:
ok 1 number(s): "451369372"
Test #3:
score: 10
Accepted
time: 10ms
memory: 1456kb
input:
200 738368650 91602659 340556191 835209882 409442204 656938221 258295858 90658347 126689369 56196859...
output:
420852999
result:
ok 1 number(s): "420852999"
Test #4:
score: 0
Time Limit Exceeded
input:
5000 300051244 685920750 122806687 315801142 33592358 339985437 262520930 380010194 687330910 550362...
output:
result:
Test #5:
score: 0
Time Limit Exceeded
input:
5000 108135249 950186870 146626753 682580494 744412491 4159 995071655 157947560 668940645 878891308 ...
output:
result:
Test #6:
score: 0
Time Limit Exceeded
input:
5000 246610103 780664789 678774940 716124165 518453378 300121008 135383319 939869983 430946660 13412...
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
300000 786856547 380253059 684225515 795392038 881780483 18437791 793783577 832330260 801624996 3466...
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
300000 951000485 930070933 3566360 157114366 150196488 611226665 123590886 950624066 472150346 57976...
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
300000 728509094 406128756 696166390 756254976 31403187 672131182 35544213 870708756 3659210 5227553...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
300000 804874490 52247250 546638789 400214210 383796618 47995083 814777769 507157584 466189162 28296...