社区讨论

照书上打为什么还报错

P3378【模板】堆参与者 5已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@mi6h311i
此快照首次捕获于
2025/11/20 04:46
4 个月前
此快照最后确认于
2025/11/20 04:46
4 个月前
查看原帖
CPP
#include <iostream>
#include <cstdio>
using namespace std;
int n,heap_size;
int heap[1000001];
int put()
{
    int now,next;
    cin>>heap[++heap_size];
    now=heap_size;
    while(now>1)
    {
        next=now>>1;
        if(heap[now]>=heap[next]) 
          return 0;
        swap(heap[now],heap[next]);
        now=next;
    }
}
int out()
{printf("%d",heap[1]);}
int del()
{
    int now,next;
    heap[1]=heap[heap_size--];
    now=1;
    while(now*2<=heap_size)
    {
        next=now*2;
        if(next<heap_size)
         if(heap[next+1]<heap[next])
          next++;
        if(heap[now]<=heap[next]) 
          break;
        swap(heap[now],heap[next]);
        now=next;
    }
}
int main()
{
    int i,j,k,l;
    scanf("%d",&n);
    for(l=1;l<=n;l++)
    {
        cin>>k;
        if(k==1) put();
        else if(k==2) out(); 
        else del();
    }
    return 0;
}

回复

4 条回复,欢迎继续交流。

正在加载回复...