萌新三十分求助

P3378 【模板】堆

```cpp #include<bits/stdc++.h> #define maxn 1000005 using namespace std; int heap[1000005],heapsize; int n,x,caozuo; void push(int x) { heapsize++; heap[heapsize]=x; int now=heapsize,next=now>>1; while(next) { if(heap[now]>=heap[next]) return; swap(heap[now],heap[next]); now=next; next=next>>1; } return; } void del() { swap(heap[1],heap[heapsize]); heapsize--; int now=1,next; while(now*2<=heapsize&&((heap[now<<1]<heap[now]||heap[now<<1+1]<heap[now]))) { next=now<<1; if(heap[next+1]<heap[next]&&next<heapsize) next++; if(heap[now]<heap[next]) return; swap(heap[now],heap[next]); now=next; } return; } int main() { cin>>n; memset(heap,0x3f,maxn); for(int i=1;i<=n;i++) { scanf("%d",&caozuo); if(caozuo==1) { scanf("%d",&x); push(x); } if(caozuo==2) printf("%d\n",heap[1]); if(caozuo==3) del(); } return 0; } ```
by AntiO2 @ 2019-03-13 17:45:47


~~建议直接priority_queue碾过~~
by gabrielliu2001 @ 2019-03-13 17:48:16


@[gabrielliu2001](/space/show?uid=114223) 想练练手写堆
by AntiO2 @ 2019-03-13 17:48:41


@[AntiO2](/space/show?uid=40513) 竞赛中很少需要手写堆的情况吧... 我反正一次都没写过...
by gabrielliu2001 @ 2019-03-13 17:54:26


@[AntiO2](/space/show?uid=40513) 所以抱歉未能帮助你
by gabrielliu2001 @ 2019-03-13 17:55:34


@[gabrielliu2001](/space/show?uid=114223) 初赛经常考,虽然也不难就是了
by encore @ 2019-03-13 17:56:15


@[encore](/space/show?uid=113385) 初赛啊...我只玩了2年,好像都没遇到XD
by gabrielliu2001 @ 2019-03-13 17:57:57


萌新嗷... 我都不信你的鬼话
by 佐倉萌香 @ 2019-03-13 18:15:04


@[注孤生QAQ](/space/show?uid=111351) 红名巨佬又来吊打蓝名蒟蒻了
by AntiO2 @ 2019-03-13 18:19:35


@[gabrielliu2001](/space/show?uid=114223) 堆顶元素没更新到然后出锅了 ```cpp while(now) { if(heap[now]>=heap[next]) return; swap(heap[now],heap[next]); now=next; next=next>>1; } ``` 改为 ```cpp while(next) { next=next>>1; if(heap[now]>=heap[next]) return; swap(heap[now],heap[next]); now=next; } ```
by AntiO2 @ 2019-03-13 18:21:23


| 下一页