Urbane_T_Rex @ 2024-10-25 12:52:40
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int q[n];
for(int i=0;i<n;i++){
cin>>q[i];
}
int mid=q[0];
cout<<mid<<endl;
priority_queue< int/*, vector<int>, greater<int>*/ > right; //小根堆
priority_queue< int > left; //大根堆
for(int i=1;i<n;i+=2){
int a=q[i];
int b=q[i+1];
if(a<mid&&b>mid){
left.push(a);
right.push(-b);
} else if(a>mid&&b<mid){
left.push(b);
right.push(-a);
} else if(a<mid&&b<mid){
left.push(a);
left.push(b);
right.push(-mid);
mid=left.top();
left.pop();
} else {
right.push(-a);
right.push(-b);
left.push(mid);
mid=-right.top();
right.pop();
}
cout<<mid<<endl;
}
}
by chenyuze_floyd @ 2024-10-25 13:20:40
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int>> pq_min;
priority_queue<int> pq_max;
int main(){
int n;
cin>>n;
vector<int> nums;
nums.push_back(-1);
for(int i=1;i<=n;i++){
int e;
cin>>e;
nums.push_back(e);
}
pq_max.push(-INT_MAX);
pq_min.push(INT_MAX);
for(int i=1;i<=n;i++){
int intend=0;
if(nums[i]<pq_max.top())
intend=1;
else if(nums[i]>pq_max.top() && nums[i]<=pq_min.top()){
if(pq_min.size()==pq_max.size()+1) intend=1;
}
if(intend==0){
if(pq_min.size()==pq_max.size()+1){
pq_max.push(pq_min.top());
pq_min.pop();
}
pq_min.push(nums[i]);
}
else{
if(pq_max.size()==pq_min.size()+1){
pq_min.push(pq_max.top());
pq_max.pop();
}
pq_max.push(nums[i]);
}
if(i%2==1){
if(pq_max.size()>pq_min.size()) cout<<pq_max.top()<<endl;
else cout<<pq_min.top()<<endl;
}
}
}
正解代码
by ILTAJ @ 2024-12-03 11:20:48
是不是没考虑是偶数的情况?我也是这样wa的