Mr_ll @ 2021-10-13 19:39:21
已AC(代码)(码风较丑,勿怪)
#include<iostream>
#include<cmath>
#include<cstdio>
#include<queue>
using namespace std;
const int N=1e5+5;
int n;
priority_queue<long long> da,xiao;
long long a[N],mid;
int main(){
scanf("%d",&n);
scanf("%lld",&a[1]);
mid=a[1];da.push(a[1]);
printf("%lld\n",a[1]);
for(int i=2;i<=n;i++){
scanf("%lld",&a[i]);
if(a[i]>mid) xiao.push(-a[i]);
else da.push(a[i]);
while(da.size()>xiao.size()){xiao.push(-da.top());da.pop();}
while(xiao.size()>da.size()){da.push(-xiao.top());xiao.pop();}
if(da.size()>xiao.size()) mid=da.top();
else mid=-xiao.top();
if(i%2==1)printf("%lld\n",mid);
}
return 0;
}
while里改成>0就不运行了
#include<iostream>
#include<cmath>
#include<cstdio>
#include<queue>
using namespace std;
const int N=1e5+5;
int n;
priority_queue<long long> da,xiao;
long long a[N],mid;
int main(){
scanf("%d",&n);
scanf("%lld",&a[1]);
mid=a[1];da.push(a[1]);
printf("%lld\n",a[1]);
for(int i=2;i<=n;i++){
scanf("%lld",&a[i]);
if(a[i]>mid) xiao.push(-a[i]);
else da.push(a[i]);
while(da.size()-xiao.size()>0){xiao.push(-da.top());da.pop();}
while(xiao.size()-da.size()>0){da.push(-xiao.top());xiao.pop();}
if(da.size()>xiao.size()) mid=da.top();
else mid=-xiao.top();
if(i%2==1)printf("%lld\n",mid);
}
return 0;
}
by Dangerou @ 2021-10-14 15:17:31
@Mr_ll
虽然我不知道怎么错的,但是我知道
新版
by hahaha1215 @ 2022-03-02 20:58:27
队列的
只要改成while((int)(da.size()-xiao.size())>0)就好了!