当前位置:网站首页>839. Simulation reactor
839. Simulation reactor
2022-07-26 09:11:00 【Hunter_ Kevin】
subject
Maintain a collection , Initially, the set is empty , The following operations are supported :
I x, Insert a number x;
PM, Output the minimum value in the current set ;
DM, Delete the minimum value in the current set ( The data guarantees that the minimum value at this time is unique );
D k, Delete the first k The number of inserts ;
C k x, Amendment No k The number of inserts , Turn it into x;
Now it's time to N operations , For all the third 2 Operations , Output the minimum value of the current set .
Input format
The first line contains integers N.
Next N That's ok , Each line contains an operation instruction , The operation instruction is I x,PM,DM,D k or C k x One of the .
Output format
For each output instruction PM, Output a result , Represents the minimum value in the current set .
Each result takes up one line .
Data range
1≤N≤105
−109≤x≤109
The data is guaranteed to be legal .
sample input :
8
I -10
PM
I -10
D 1
C 2 8
I 6
PM
DM
sample output :
-10
6
Code
#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]]);// The current location of the swap storage heap node , That is, update a The new location of the node and b The new location of the node
swap(hp[a], hp[b]);// In exchange for a Node on ph The value of the index in the array , Follow b Nodes in the ph The value of the index in the array
swap(h[a], h[b]);// The values of the two nodes of the swap heap
}
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)// If you need to switch down
{
exchange(t, u);// The smaller of the parent node and the two child nodes in the swap heap
down(t);// Continue to swap the smaller of the three values in the heap
}
}
void up(int u)
{
while(u/2 && h[u] < h[u/2])// If u The parent node of the node exists , also u The value of the node <u/2 The value of the node
{
exchange(u, u/2);// In the swap heap u and u/2 The nodes of the location
u/=2;// Continue to judge the parent node
}
}
int main()
{
int n;
cin >> n;
int m = 0;
while (n -- )
{
char op[5];
int k, x;
cin >> op;
if(!strcmp(op,"I"))// If you need to insert a new heap node
{
cin >> x;
cnt++;// Number of heap nodes
m ++ ;// Number of inserts , As index
ph[m] = cnt; // The first m The position of the heap node inserted for the second time is cnt, Because the value of the heap element is h[cnt]
hp[cnt] = m; // The first cnt Heap nodes are ph The index in the array is m, That is, the previous line of code ph[m] Medium m, That is to establish a two-way mapping relationship
h[cnt] = x;// Assign a value to the inserted heap node
up(cnt);// The new node inserted at the bottom of the heap goes up
}
else if(!strcmp(op,"PM"))
{
cout<<h[1]<<endl;
}
else if(!strcmp(op,"DM"))
{
exchange(1,cnt--);// Exchange the top element of the heap with the end element of the heap
down(1);//down operation
}
else if(!strcmp(op,"D"))
{
cin >> k;
k = ph[k];// The first k The current position of the number of inserts
exchange(k,cnt--);// The first k The current position of the number follows cnt As a parameter , swapping
down(k);
up(k);
}
else if(!strcmp(op,"C"))
{
cin >> k >> x;
k = ph[k];// For the first k The current position of the number of inserts
h[k] = x;// Amendment No k An inserted element
down(k);
up(k);
}
}
return 0;
}
边栏推荐
- 原根与NTT 五千字详解
- Center an element horizontally and vertically
- Laravel框架日志文件存放在哪里?怎么用?
- Mutual transformation of array structure and tree structure
- Pop up window in Win 11 opens with a new tab ---firefox
- Clean the label folder
- 分布式跟踪系统选型与实践
- [eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
- 对象型的集合按某个属性的值进行去重
- NPM add source and switch source
猜你喜欢
Day06 homework - skill question 6
Probability model in machine learning
Qtcreator reports an error: you need to set an executable in the custom run configuration
The Child and Binary Tree-多项式开根求逆
NFT与数字藏品到底有何区别?
220. Presence of repeating element III
Study notes of dataX
(2006,Mysql Server has gone away)问题处理
公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
随机推荐
JS - DataTables 关于每页显示数的控制
PHP page value transfer
Study notes of canal
CF1481C Fence Painting
Database operation skills 7
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
zsh: command not found: nvm
JS file import of node
Laravel框架日志文件存放在哪里?怎么用?
堆外内存的使用
Database operation skills 6
Datawhale panda book has been published!
Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
codeforces dp合集
分布式跟踪系统选型与实践
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
unity简易消息机制
JVM触发minor gc的条件
Simple message mechanism of unity