社区讨论
对顶堆 WA 求 HACK
SP16254RMID2 - Running Median Again参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m6pxputl
- 此快照首次捕获于
- 2025/02/04 11:43 去年
- 此快照最后确认于
- 2025/02/04 12:29 去年
CPP
#include<iostream>
#include<queue>
using namespace std;
void solve() {
priority_queue<int>q1; priority_queue<int,vector<int>,greater<int> >q2; int cnt=0;
while(true) {
int n; cin>>n;
if(!n) return;
if(!(~n)) {
cout<<q1.top()<<'\n',q1.pop(),cnt--;
while(q1.size()<(cnt+1)/2) q1.push(q2.top()),q2.pop();
while(q1.size()>(cnt+1)/2) q2.push(q1.top()),q1.pop();
}
else {
cnt++,q1.push(n);
while(q1.size()<(cnt+1)/2) q1.push(q2.top()),q2.pop();
while(q1.size()>(cnt+1)/2) q2.push(q1.top()),q1.pop();
}
}
}
int main() {
ios::sync_with_stdio(false),cin.tie(nullptr);
int t; cin>>t;
while(t--) solve();
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...