当前位置:网站首页>Heap acwing 839 Simulated reactor
Heap acwing 839 Simulated reactor
2022-07-02 12:42:00 【T_ Y_ F666】
Pile up AcWing 839. Simulation reactor
Original link
AcWing 839. Simulation reactor
Algorithm tags
Pile up
Ideas
Because the title needs to support random modification and deletion , Therefore, each node mapping needs to be stored 
Excerpt from the solution
The exchange process

The corresponding code
void heap_swap(int a, int b)
{
// As shown in the blue dotted line
swap(ph[hp[a]],ph[hp[b]]);
// As shown in the green dotted line
swap(hp[a], hp[b]);
// In the pile a, b At two o 'clock
swap(h[a], h[b]);
}
Code
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = 100010;
// ph[k] = i To indicate , The first k The inserted number is in h[] Corresponding i How much is the
// hp{k] = i To indicate , Store... For each heap array k The subscript corresponds to i
int h[N], ph[N], hp[N], cnt;
void heap_swap(int a, int b)
{
// As shown in the blue dotted line
swap(ph[hp[a]],ph[hp[b]]);
// As shown in the green dotted line
swap(hp[a], hp[b]);
// In the pile a, b At two o 'clock
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 ++ ;
// Add mapping
ph[m] = cnt, hp[cnt] = m;
// Insert numbers x
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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- 2.6 using recursion and stack - [tower of Hanoi problem]
- Sweetheart leader: Wang Xinling
- PR 2021 quick start tutorial, learn about the and functions of the timeline panel
- Find the common ancestor of any two numbers in a binary tree
- BOM DOM
- Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
- 百款拿来就能用的网页特效,不来看看吗?
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
- FBX import under ue4/ue5 runtime
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
猜你喜欢

NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
![[ybtoj advanced training guidance] judgment overflow [error]](/img/be/bbe357ac2f2a8839afc5af47db88d0.jpg)
[ybtoj advanced training guidance] judgment overflow [error]

Floyd AcWing 854. Floyd求最短路

Sparkcontext: error initializing sparkcontext solution

JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)

AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning

MySQL and PostgreSQL methods to grab slow SQL
![1380. Lucky numbers in the matrix [two-dimensional array, matrix]](/img/8c/c050af5672268bc7e0df3250f7ff1d.jpg)
1380. Lucky numbers in the matrix [two-dimensional array, matrix]

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

分布式机器学习框架与高维实时推荐系统
随机推荐
Use sqoop to export ads layer data to MySQL
Use MySQL events to regularly perform post seven world line tasks
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
How to write a pleasing English mathematical paper
LeetCode—剑指 Offer 59 - I、59 - II
堆 AcWing 838. 堆排序
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
软件测试面试题-2022年大厂面试题合集
线性DP AcWing 895. 最长上升子序列
哈希表 AcWing 841. 字符串哈希
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
MySQL and PostgreSQL methods to grab slow SQL
"As a junior college student, I found out how difficult it is to counter attack after graduation."
Fastdateformat why thread safe
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
计数类DP AcWing 900. 整数划分
Docsify deploy IIS
中国交通标志检测数据集
Drools terminates the execution of other rules after executing one rule
LeetCode—剑指 Offer 51. 数组中的逆序对