当前位置:网站首页>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 
边栏推荐
- Sweetheart leader: Wang Xinling
- JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
- BOM DOM
- Redis sentinel mechanism and configuration
- 线性DP AcWing 898. 数字三角形
- [I'm a mound pytorch tutorial] learning notes
- 模块化 CommonJS ES Module
- Fastdateformat why thread safe
- 浏览器存储方案
- [ybtoj advanced training guidance] cross the river [BFS]
猜你喜欢

BOM DOM
![[ybtoj advanced training guidance] judgment overflow [error]](/img/be/bbe357ac2f2a8839afc5af47db88d0.jpg)
[ybtoj advanced training guidance] judgment overflow [error]
![[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol](/img/13/9002244555ebe8a61660c2506993fa.png)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol

模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计

计数类DP AcWing 900. 整数划分

Distributed machine learning framework and high-dimensional real-time recommendation system

Find the common ancestor of any two numbers in a binary tree

Simple use of drools decision table

Multiply LCA (nearest common ancestor)

"As a junior college student, I found out how difficult it is to counter attack after graduation."
随机推荐
IPhone 6 plus is listed in Apple's "retro products" list
BOM DOM
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
arcgis js 4. Add pictures to x map
In development, why do you find someone who is paid more than you but doesn't write any code?
线性DP AcWing 898. 数字三角形
Go learning notes - multithreading
FBX import under ue4/ue5 runtime
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
What data types does redis have and their application scenarios
Simple use of drools decision table
Docker compose configuration mysql, redis, mongodb
线性DP AcWing 899. 编辑距离
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
Redis introduction, scenario and data type
Sub thread get request
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
ArrayList与LinkedList效率的对比
防抖 节流