当前位置:网站首页>堆 AcWing 839. 模拟堆
堆 AcWing 839. 模拟堆
2022-07-02 09:43:00 【T_Y_F666】
堆 AcWing 839. 模拟堆
原题链接
算法标签
堆
思路
由于题目需要支持随机修改删除, 因此需要存储每个节点映射
摘自该题解
交换过程
对应代码
void heap_swap(int a, int b)
{
// 如图中蓝色虚线
swap(ph[hp[a]],ph[hp[b]]);
// 如图中绿色虚线
swap(hp[a], hp[b]);
// 堆中a, b两点
swap(h[a], h[b]);
}
代码
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = 100010;
// ph[k] = i来指明,第k插入的数字在h[]中对应的i是多少
// hp{k] = i来指明,存储每个堆数组的k下标对应i
int h[N], ph[N], hp[N], cnt;
void heap_swap(int a, int b)
{
// 如图中蓝色虚线
swap(ph[hp[a]],ph[hp[b]]);
// 如图中绿色虚线
swap(hp[a], hp[b]);
// 堆中a, b两点
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 ++ ;
// 增加映射关系
ph[m] = cnt, hp[cnt] = m;
// 插入数字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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 中国交通标志检测数据集
- drools执行String规则或执行某个规则文件
- 寻找二叉树中任意两个数的公共祖先
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- LeetCode—剑指 Offer 51. 数组中的逆序对
- 甜心教主:王心凌
- [C language] convert decimal numbers to binary numbers
- 深拷贝 事件总线
- The differences and relationships among port, targetport, nodeport and containerport in kubenetes
猜你喜欢
MySQL与PostgreSQL抓取慢sql的方法
分布式机器学习框架与高维实时推荐系统
async/await 异步函数
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
AI中台技术调研
VLAN experiment
BOM DOM
Jenkins user rights management
[C language] convert decimal numbers to binary numbers
随机推荐
CDA data analysis -- common knowledge points induction of Excel data processing
Embedded Software Engineer career planning
The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
Writing method of then part in drools
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
怎样写一篇赏心悦目的英文数学论文
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
AI mid stage technology research
SparkContext: Error initializing SparkContext解决方法
Maximum profit of jz63 shares
Go learning notes - go based interprocess communication
线性DP AcWing 899. 编辑距离
Fastdateformat why thread safe
计数类DP AcWing 900. 整数划分
BOM DOM
线性DP AcWing 895. 最长上升子序列
Jenkins voucher management
排序---
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
Calculate the maximum path sum of binary tree