原题解
![在这里插入图片描述](https://img-blog.csdnimg.cn/505429e0aa614d6e8a165a4d1597a515.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbWluZ3l1MTk=,size_20,color_FFFFFF,t_70,g_se,x_16)
![在这里插入图片描述](https://img-blog.csdnimg.cn/68c1b07ccf52422f9be4704343d0857e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbWluZ3l1MTk=,size_20,color_FFFFFF,t_70,g_se,x_16)
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int n,m,s;
int h[N];
void down(int u)
{
int t=u;
if(u*2<=s&&h[u*2]<h[t]) t=u*2;
if(u*2+1<=s&&h[u*2+1]<h[t]) t=u*2+1;
if(u!=t)
{
swap(h[t],h[u]);
down(t);
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>h[i];
s=n;
for(int i=n/2;i;i--) down(i);
while(m--)
{
cout<<h[1]<<" ";
h[1]=h[s];
s--;
down(1);
}
return 0;
}
作者:Tsukinousag1
链接:https:
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/a338c28a6591427d8a40f33b9f09c3a2.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbWluZ3l1MTk=,size_20,color_FFFFFF,t_70,g_se,x_16)
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1e5+10;
char str[2];
int hp[N],ph[N],h[N];
int SIZE;
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 u)
{
int t=u;
if(u*2<=SIZE&&h[u*2]<h[t]) t=u*2;
if(u*2+1<=SIZE&&h[u*2+1]<h[t]) t=u*2+1;
if(t!=u)
{
heap_swap(t,u);
down(t);
}
}
void up(int u)
{
while(u/2&&h[u/2]<h[u])
{
heap_swap(u/2,u);
u/=2;
}
}
int main()
{
int n,m=0;
cin>>n;
while(n--)
{
int x,k;
cin>>str;
if(!strcmp(str,"I"))
{
scanf("%d",&x);
SIZE++;
m++;
hp[SIZE]=m,ph[m]=SIZE;
h[SIZE]=x;
up(SIZE);
}
else if(!strcmp(str,"PM"))cout<<h[1]<<endl;
else if(!strcmp(str,"DM"))
{
heap_swap(1,SIZE);
SIZE--;
down(1);
}
else if(!strcmp(str,"D"))
{
scanf("%d",&k);
k=hp[k];
heap_swap(k,SIZE);
up(k);
down(k);
}
else if(!strcmp(str,"C"))
{
scanf("%d%d",&k,&x);
k=hp[k];
h[k]=x;
up(k);
down(k);
}
}
return 0;
}
作者:Tsukinousag1
链接:https:
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。