普通对顶堆
title: 普通对顶堆
categories:
- ICPC
tags:
- null
abbrlink: d860667d
date: 2024-06-27 00:00:00
动态维护第k大
priority_queue<int> a; //大根堆
priority_queue<int,vector<int>,greater<int> > b;
for(int i=1; i<=n; i++){
int x; scanf("%d",&x);
if(b.empty()||x>=b.top()) b.push(x); //插入
else a.push(x);
int k=max(1,i*w/100); //第k大
while(b.size()>k) a.push(b.top()), b.pop(); //调整
while(b.size()<k) b.push(a.top()), a.pop();
printf("%d ", b.top()); //取值
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 爱飞鱼的blog!