当前位置:网站首页>839. 模拟堆
839. 模拟堆
2022-07-26 09:05:00 【Hunter_Kevin】
题目
维护一个集合,初始时集合为空,支持如下几种操作:
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
代码
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010;
int h[N], cnt;
int hp[N], ph[N];
void exchange(int a, int b)
{
swap(ph[hp[a]], ph[hp[b]]);//交换存储堆节点的当前所在位置,即更新a节点所在的新的位置和b节点所在的新的位置
swap(hp[a], hp[b]);//交换a节点在在ph数组中的索引的值,跟b节点在ph数组中的索引的值
swap(h[a], h[b]);//交换堆的两个节点的值
}
void down(int u)
{
int t = u;
if(2*u <= cnt && h[2*u] < h[t]) t = 2*u;
if(2*u+1 <= cnt && h[2*u+1] < h[t]) t = 2*u+1;
if(t != u)//如果需要往下交换
{
exchange(t, u);//交换堆中父节点跟两个子节点中的较小值
down(t);//继续交换堆中三个值中的较小值
}
}
void up(int u)
{
while(u/2 && h[u] < h[u/2])//如果u节点的父节点存在,并且u节点的值<u/2节点的值
{
exchange(u, u/2);//交换堆中u和u/2位置的节点
u/=2;//继续判断父节点
}
}
int main()
{
int n;
cin >> n;
int m = 0;
while (n -- )
{
char op[5];
int k, x;
cin >> op;
if(!strcmp(op,"I"))//如果需要插入一个新的堆节点
{
cin >> x;
cnt++;//堆节点的个数
m ++ ;//插入的次数,作为索引
ph[m] = cnt; //第m次插入的堆节点的位置为cnt, 因为此时堆元素的值为h[cnt]
hp[cnt] = m; //第cnt个堆节点在ph数组中的索引为m,即上一行代码ph[m]中的m,即建立双向映射关系
h[cnt] = x;//给插入的堆节点赋值
up(cnt);//插入到堆底的新节点往上走
}
else if(!strcmp(op,"PM"))
{
cout<<h[1]<<endl;
}
else if(!strcmp(op,"DM"))
{
exchange(1,cnt--);//交换堆顶元素跟堆的末尾元素
down(1);//down操作
}
else if(!strcmp(op,"D"))
{
cin >> k;
k = ph[k];//第k个插入的数的当前位置
exchange(k,cnt--);//把第k个数当前所在的位置跟cnt作为参数,进行交换
down(k);
up(k);
}
else if(!strcmp(op,"C"))
{
cin >> k >> x;
k = ph[k];//获取第k个插入的数的当前位置
h[k] = x;//修改第k个插入的元素
down(k);
up(k);
}
}
return 0;
}
边栏推荐
- [recommended collection] MySQL 30000 word essence summary index (II) [easy to understand]
- 巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
- Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world
- 《Datawhale熊猫书》出版了!
- [eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
- Web overview and b/s architecture
- 【LeetCode数据库1050】合作过至少三次的演员和导演(简单题)
- ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
- PHP 之 Apple生成和验证令牌
- CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
猜你喜欢
李沐d2l(五)---多层感知机
at、crontab
ES6模块化导入导出)(实现页面嵌套)
redis原理和使用-安装和分布式配置
Qtcreator reports an error: you need to set an executable in the custom run configuration
Nuxt - 项目打包部署及上线到服务器流程(SSR 服务端渲染)
Database operation topic 2
机器学习中的概率模型
Day06 homework - skill question 7
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
随机推荐
Nuxt - 项目打包部署及上线到服务器流程(SSR 服务端渲染)
SQL入门——组合表
对标注文件夹进行清洗
node的js文件引入
day06 作业--技能题2
围棋智能机器人阿法狗,阿尔法狗机器人围棋
Flask project learning (I) -- sayhello
Sklearn machine learning foundation (linear regression, under fitting, over fitting, ridge regression, model loading and saving)
JDBC数据库连接池(Druid技术)
数据库操作 技能6
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
Regular expression: judge whether it conforms to USD format
What are the differences in the performance of different usages such as count (*), count (primary key ID), count (field) and count (1)? That's more efficient
Laravel框架日志文件存放在哪里?怎么用?
【无标题】
209. Subarray with the smallest length
李沐d2l(四)---Softmax回归
Study notes of automatic control principle -- correction and synthesis of automatic control system
Pop up window in Win 11 opens with a new tab ---firefox
ext3文件系统的一个目录下,无法创建子文件夹,但可以创建文件