当前位置:网站首页>839. Simulated heap
839. Simulated heap
2022-07-30 06:05:00 【Zhang Xueheng】
1:题目
维护一个集合,初始时集合为空,支持如下几种操作:
I x,插入一个数 x;
PM,输出当前集合中的最小值;
DM,删除当前集合中的最小值(数据保证此时的最小值唯一);
D k,删除第 k 个插入的数;
C k x,修改第 k 个插入的数,将其变为 x;
现在要进行 N 次操作,对于所有第 2 个操作,输出当前集合的最小值.
输入格式
第一行包含整数 N.
接下来 N 行,每行包含一个操作指令,操作指令为 I x,PM,DM,D k 或 C k x 中的一种.
输出格式
对于每个输出指令 PM,输出一个结果,表示当前集合中的最小值.
每个结果占一行.
数据范围
1≤N≤105
−109≤x≤109
数据保证合法.
输入样例:
8
I -10
PM
I -10
D 1
C 2 8
I 6
PM
DM
输出样例:
-10
6
2:代码实现
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=100010;
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 u)
{
int t=u;
if(u*2<=cnt && h[u*2]<h[t] )t=u*2;
if(u*2 + 1<=cnt && h[u*2+1]<h[t])t=u*2+1;
if(u!=t)
{
heap_swap(u,t);
down(t);
}
}
void up(int u)
{
while(u/2&&h[u]<h[u/2])
{
heap_swap(u,u/2);
u>>=1;
}
}
int main()
{
int n,m=0;
scanf("%d",&n);
while (n -- ){
char op[5];
int k,x;
scanf("%s",op);
if(!strcmp(op,"I"))
{
scanf("%d",&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);
cnt--;
down(1);
}
else if(!strcmp(op,"D"))
{
scanf("%d",&k);
k=ph[k];
heap_swap(k,cnt);
cnt--;
up(k);
down(k);
}
else
{
scanf("%d%d",&k,&x);
k=ph[k];
h[k]=x;
up(k);
down(k);
}
}
return 0;
}
边栏推荐
猜你喜欢

Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
![[Image processing] Image skeleton extraction based on central axis transformation with matlab code](/img/56/cfef9389291fa39612d55a07e1dbb3.png)
[Image processing] Image skeleton extraction based on central axis transformation with matlab code

从想当亿万富翁到职场、创业、爱情、抑郁、学医学武,我的程序人生

mysql 中 in 的用法

Teach you how to design a CSDN system

idea设置自动带参数的方法注释(有效)

navicat无法连接mysql超详细处理方法

curl (7) Failed connect to localhost8080; Connection refused
![[Redis Master Cultivation Road] Jedis - the basic use of Jedis](/img/e3/0c6efd03432a01f857796f0bf648ef.png)
[Redis Master Cultivation Road] Jedis - the basic use of Jedis

MySQL模糊查询性能优化
随机推荐
Teach you how to design a CSDN system
破纪录者(Google Kickstart2020 Round D Problem A)
是时候不得不学英语了,技多不压身,给自己多条路
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
How MySQL to prepare SQL pretreatment (solve the query IN SQL pretreatment can only query out the problem of a record)
面试题 17.13. 恢复空格(字典树)
WeChat payment and payment callback
从字节码角度带你彻底理解i++与++i
MySql fuzzy query Daquan
最新版MySQL 8.0 的下载与安装(详细教程)
2022 Pengcheng Cup web
路径依赖:穷人很难逆袭突破的科学道理
《后浪》程序员版,献给新一代程序员的演讲,何冰《后浪》演讲模仿秀
Within the SQL connection table (link connections, left or right, cross connection, full outer join)
MySQL Basics (DDL, DML, DQL)
What is SOA (Service Oriented Architecture)?
MySQL(3)
瑞吉外卖项目:新增菜品与菜品分页查询
图形镜像对称(示意图)
cookie和session区别