题目链接 https://www.acwing.com/problem/content/841/
错误的代码:
#include<iostream>
#include<string.h>
using namespace std;
const int N=1e5+10;
int h[N],ph[N],hp[N],cnt;
void heap_swap(int a,int b)
{
swap( ph[hp[a]],ph[hp[b]]);
swap(hp[a],hp[b]);
swap(h[a],h[b]);
}
void down(int x)
{
int t=x;
if(2*x<=cnt && h[2*x]<h[t])t=2*x;
if(2*x+1<=cnt && h[2*x+1]<h[t])t=2*x+1;
if(x!=t)
{
heap_swap(t,x);
down(t);
}
}
void up(int x)
{
if(x/2&&h[x/2]>h[x])
heap_swap(x,x/2);
x/=2;
}
int main()
{
int n,x,m=0;
while(n--)
{
char op[5];
scanf("%s",op);
if(!strcmp(op,"I"))
{
cin>>x;
cnt++;m++;
ph[m]=cnt;hp[cnt]=m;
h[cnt]=x;up(cnt);
}
else if(!strcmp(op,"PM"))
{
printf("%d\n",h[1]);
}
else if(!strcmp(op,"DM"))
{
heap_swap(1,cnt--);down(1);
}
else if(!strcmp(op,"D"))
{
int k;
cin>>k;
heap_swap(ph[k],cnt--);down(ph[k]);up(ph[k]);
}
else
{
int k,x;
cin>>k>>x;
h[hp[k]]=x;down(ph[k]);up(ph[k]);
}
}
return 0;
}
为什么会运行超时
n
都没输入
估计哪里错了