【模板】小根堆
阿新 • • 發佈:2017-10-28
names ret %d opened code spa ace cnblogs 一個空格
因為根的實現方法(優先隊列)默認為大根堆,即從大到小排列,所以在需要的時候需要手寫小根堆。
題目描述
如題,初始小根堆為空,我們需要支持以下3種操作:
操作1: 1 x 表示將x插入到堆中
操作2: 2 輸出該小根堆內的最小數
操作3: 3 刪除該小根堆內的最小數
輸入輸出格式
輸入格式:
第一行包含一個整數N,表示操作的個數
接下來N行,每行包含1個或2個正整數,表示三種操作,格式如下:
操作1: 1 x
操作2: 2
操作3: 3
輸出格式:
包含若幹行正整數,每行依次對應一個操作2的結果。
輸入輸出樣例
輸入樣例#1: 復制5 1 2 1 5 2 3 2輸出樣例#1: 復制
2 5
說明
時空限制:1000ms,128M
數據規模:
對於30%的數據:N<=15
對於70%的數據:N<=10000
對於100%的數據:N<=1000000(註意是6個0。。。不過不要害怕,經過編者實測,堆是可以AC的)
樣例說明:
故輸出為2、5
1 #include <iostream> 2 #include <cmath> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 using namespacepriority_queuestd; 8 struct data 9 { 10 int x,y,z; 11 bool operator <(const data&o)const 12 { 13 return o.z<z; /**/ 14 } 15 }; 16 17 int main() 18 { 19 int n; 20 scanf("%d",&n); 21 priority_queue<int,vector<int>,greater<int> /*這兩個 >之間一定要加一個空格*/> q;22 for(int i=1;i<=n;i++) 23 { 24 int c; 25 scanf("%d",&c); 26 if(c==1) 27 { 28 int x; 29 scanf("%d",&x); 30 q.push(x); 31 } 32 else if(c==2) printf("%d\n",q.top());/**/ 33 else q.pop(); 34 } 35 system("pause"); 36 return 0; 37 }
【模板】小根堆