当前位置:网站首页>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
边栏推荐
- Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
- H5 to app
- LeetCode—剑指 Offer 37、38
- FBX import under ue4/ue5 runtime
- 高性能纠删码编码
- LeetCode—<动态规划专项>剑指 Offer 19、49、60
- Writing method of then part in drools
- Wechat official account payment prompt MCH_ ID parameter format error
- 8A 同步降压稳压器 TPS568230RJER_规格信息
- [ybtoj advanced training guide] similar string [string] [simulation]
猜你喜欢
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
Docker-compose配置Mysql,Redis,MongoDB
哈希表 AcWing 840. 模拟散列表
Deep Copy Event bus
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
通过反射执行任意类的任意方法
Redis sentinel mechanism and configuration
arcgis js 4. Add pictures to x map
堆 AcWing 839. 模拟堆
随机推荐
Fluent fluent library encapsulation
Visual studio efficient and practical extension tools and plug-ins
3 A VTT端接 稳压器 NCP51200MNTXG资料
FBX import under ue4/ue5 runtime
Is the neural network (pinn) with embedded physical knowledge a pit?
About asp Net MVC project in local vs running response time is too long to access, the solution!
LeetCode—剑指 Offer 37、38
Leetcode - Sword finger offer 59 - I, 59 - II
线性DP AcWing 897. 最长公共子序列
Performance tuning project case
Record the range of data that MySQL update will lock
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
H5 to app
8 examples of using date commands
JDBC 预防sql注入问题与解决方法[PreparedStatement]
VLAN experiment
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
CPU指令集介绍
js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用