wls的数据结构-堆
堆
没有.clear()操作
多路归并,mlogm
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5+10;
priority_queue<PII, vector<PII>, greater<PII> > q;
struct Node{
int x, y;
}node[N];
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i ++){
int x, y; scanf("%d %d", &x, &y);
node[i] = {x, y};
q.push({y, i});
}
vector<int> res;
int m; cin >> m;
while(m --){
auto u = q.top(); q.pop();
res.push_back(u.first);
q.push({((u.first - node[u.second].y) / node[u.second].x + 1) * node[u.second].x + node[u.second].y, u.second});
}
for(auto c : res) printf("%d ", c);
puts("");
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
typedef pair<int, int> PII;
int mon[N];
int main(){
priority_queue<PII, vector<PII>, greater<PII> > qmin;
priority_queue<PII> qmax;
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i ++) mon[i] = 100;
for(int i = 1; i <= n; i ++){
qmin.push({100, i});
qmax.push({100, i});
}
while(m -- ){
int op; scanf("%d", &op);
if(op == 1){
int p, x; scanf("%d %d", &p, &x);
mon[p] += x;
qmin.push({mon[p], p});
qmax.push({mon[p], p});
}
else{
int maxn, minn;
while(true){
auto u = qmin.top();
if(mon[u.second] == u.first){
minn = u.first;
break;
}
else qmin.pop();
}
while(true){
auto u = qmax.top();
if(mon[u.second] == u.first){
maxn = u.first;
break;
}
else qmax.pop();
}
printf("%d %d\n", maxn, minn);
}
}
return 0;
}
动态中位数
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
priority_queue< int > maxn;
priority_queue< int, vector< int >, greater<int> > minn;
int n; cin >> n;
for(int i = 1; i <= n; i ++){
int x; scanf("%d", &x);
maxn.push(x);
while(maxn.size() > minn.size() + 1){
minn.push(maxn.top());
maxn.pop();
}
while(minn.size() && maxn.top() > minn.top()){
auto t1 = maxn.top(), t2 = minn.top();
maxn.pop(); minn.pop();
minn.push(t1), maxn.push(t2);
}
if(i % 2) printf("%d ", maxn.top());
}
puts("");
return 0;
}