Pandas核心开发者Wes McKinney的故事:一个开源工具如何从华尔街量化需求中诞生
2026/6/7 7:11:17
STL定义:
priority_queue<int> heap;priority_queue<int,vector<int>,greater<int> > heap;自定义比较器:
struct compare{ bool operater(int a,int b){ return a>b; } }; priority_queue<int,vector<int>,compare> pq;最后一行的堆是小根堆,
而
return a<b;与排序的顺序相反!
1.模板题:
提交答案加入题单复制题目
提交200.08k
通过86.76k
时间限制1.00s
内存限制512.00MB
题目编号P3378
给定一个数列,初始为空,请支持下面三种操作:
第一行是一个整数,表示操作的次数 n。
接下来 n 行,每行表示一次操作。每行首先有一个整数 op 表示操作类型。
对于每个操作 2,输出一行一个整数表示答案。
输入 #1复制
5 1 2 1 5 2 3 2
输出 #1复制
2 5
【数据规模与约定】
#include <bits/stdc++.h> using namespace std; int heap[1000005]; int pos=1; void push(int x) { heap[pos]=x; int kid=pos; int dad=kid/2; while (dad>=1 and heap[kid]<heap[dad]) { swap(heap[kid],heap[dad]); kid=dad; dad=kid/2; } pos++; } void down(int n) { int self=n; while(true) { int left=2*self; int right=2*self+1; int smallest=self; if(left<pos and heap[left]<heap[smallest]) { smallest=left; } if(right<pos and heap[right]<heap[smallest]) { smallest=right; } if(smallest!=self) { swap(heap[self],heap[smallest]); self=smallest; }else{ break; } } } void del() { if (pos<=1) return; swap(heap[1],heap[pos-1]); pos--; down(1); } int main() { int n; cin>>n; while(n--) { int op; cin>>op; switch (op) { case 1: { int x; cin>>x; push(x); break; } case 2:{ if(pos>1) { cout<<heap[1]<<endl; } break; } case 3:{ del(); break; } } } return 0; }