P3378 堆の模板
如果不是可并堆/带修堆/卡常题,一般都用优先队列实现。
很多O(nlogn)过不了的题都可以用蚯蚓的套路来实现!!!
优先队列带修用延迟删除法。
堆,可以简单的用优先队列来实现,也可以自己手打。
#include <cstdio>
#include <algorithm>
using namespace std; const int N = ;
long long int heap[*N+],top;
void down(int);
void up(int a)
{
while(heap[a]<heap[a>>] && a!=) swap(heap[a],heap[a>>]),a=a>>;
down(a);
return;
}
void down(int a)
{
if((a<<)>top) return;///这里的括号不能省;这里的等号不能加。调了一个中午的教训啊
if((a<<)==top)
{
if(heap[a]>heap[top]) swap(heap[a],heap[top]);
return;
}
if(heap[a]<=heap[a<<] && heap[a]<=heap[a<<|]) return;
if(heap[a]<heap[a<<])
{
swap(heap[a],heap[a<<|]);
down(a<<|);
return;
}
if(heap[a]<heap[a<<|])
{
swap(heap[a],heap[a<<]);
down(a<<);
return;
}
if(heap[a<<]<heap[a<<|])
{
swap(heap[a],heap[a<<]);
down(a<<);
return;
}
else
{
swap(heap[a],heap[a<<|]);
down(a<<|);
return;
}
}
void add(int a)
{
heap[++top]=a;
up(top);
return;
}
void del()
{
swap(heap[],heap[top]);
top--;
if(top<=) return;
down();
return;
}
int main()
{
int n;
scanf ("%d",&n);
int flag,x;
for(int i=;i<=n;i++)
{
scanf ("%d",&flag); if(flag==) printf("%d\n",heap[]);
else if(flag==) del();
else scanf ("%d",&x),add(x);
} return ;
}
模板在此!!!
想不到居然写了70行......
之前写的跟屎一样......
最新模板在此:
#include <cstdio>
#include <algorithm>
using std::swap;
typedef long long LL;
const int N = ; struct SmallHeap {
LL h[N];
int top;
SmallHeap() {
top = ;
}
inline void up(int p) {
while(p != && h[p] < h[p >> ]) {
swap(h[p], h[p >> ]);
p = p >> ;
}
return;
}
inline void down(int p) {
int s = p << ;
while(s <= top) {
if(s < top && h[s] > h[s | ]) {
s = s | ;
}
if(h[s] < h[p]) {
swap(h[s], h[p]);
p = s;
s = p << ;
}
else break;
}
return;
}
inline void insert(LL a) {
h[++top] = a;
up(top);
return;
}
inline void del(int p) {
h[p] = h[top--];
up(p);
down(p);
return;
}
inline LL gettop() {
return h[];
}
inline void pop() {
h[] = h[top--];
down();
return;
}
}heap; int main() {
LL n, x;
scanf("%lld", &n);
for(int i = ; i <= n; i++) {
scanf("%lld", &x);
heap.insert(x);
}
LL ans = ;
for(int i = ; i < n; i++) {
LL temp = heap.gettop();
heap.pop();
temp += heap.gettop();
heap.pop();
ans += temp;
heap.insert(temp);
}
printf("%lld", ans);
return ;
}
小根堆
P3378 堆の模板的更多相关文章
- 洛谷P3378 【模板】堆
P3378 [模板]堆 160通过 275提交 题目提供者HansBug 标签 难度普及- 提交 讨论 题解 最新讨论 经实际测试 堆的数组开3000- 题目有个问题 为什么这个按课本堆标准打的- ...
- P3378 【模板】堆 (内含左偏树实现)
P3378 [模板]堆 题解 其实就是一个小根堆啦,STL就可以解决,但是拥有闲情雅致的我学习了Jelly_Goat的左偏树,增加了代码长度,妙啊 Solution 1 STL STL 里面prior ...
- P3378 堆(模板)
P3378 [模板]堆 题目描述 给定一个数列,初始为空,请支持下面三种操作: 给定一个整数 x,请将 x 加入到数列中. 输出数列中最小的数. 删除数列中最小的数(如果有多个数最小,只删除 1 个) ...
- 可并堆模板题-mergeable heap
Description 有n个点,第i个点标号为i,有两种操作:0 x y 表示把x所在堆和y所在堆合并.1 x 表示询问x所在堆的最小权. Input 第一行两个整数n,m,表示有n个点m个操作. ...
- 【luogu P3378 堆】 模板
题目链接:https://www.luogu.org/problemnew/show/P3378 是堆的模板...我懒,STL da fa is good #include <iostream& ...
- Luogu P3378 【模板】堆
((^ 0.0 ^) )~ 堆是一个完全二叉树,对于小根堆,所有父节点<=子节点,下标就和线段树是一样的 在STL里就是优先队列 只有堆顶元素可以操作(询问或弹出). 加入新元素时x,he ...
- 堆模板(pascal)洛谷P3378
题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...
- 洛谷 P3378 【模板】堆
如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: 第一行包含 ...
- P3378 堆【模板】 洛谷
https://www.luogu.org/problem/show?pid=3378 题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 ...
随机推荐
- 在浏览器上安装 Vue Devtools工具
Vue.js devtools是基于google chrome浏览器的一款调试vue.js应用的开发者浏览器扩展,可以在浏览器开发者工具下调试代码. 1)首先在github下载devtools源码,地 ...
- springboot项目小总结
使用模板引擎 thyemlef 可以直接将 html文件进行导入 loginhtml文件 html中常用的表达式 <link href="asserts/css/signin.cs ...
- 在linux系统中实现各项监控的关键技术(2)--内核态与用户态进程之间的通信netlink
Netlink 是一种在内核与用户应用间进行双向数据传输的非常好的方式,用户态应用使用标准的 socket API 就可以使用 netlink 提供的强大功能,内核态需要使用专门的内核 API 来使用 ...
- Essential Phone刷机教程
安装fastboot驱动(Essential-PH1-WindowsDrivers) 下载ADB刷机指令工具:platform-tools(ADB): 进入开发者选项,打开 USB 调试,OEM解锁选 ...
- tomcat9 点击bin目录下的startup.bat一闪而过
我装的是tomcat9免安装版,jdk版本是11,之后去tomcat bin目录下点击startup.bat闪退(好吧,只有想办法解决了) 博客中的解决办法五花八门,什么环境变量没配好....不过都不 ...
- U68641 划水(swim.pas/c/cpp)
U68641 划水(swim.pas/c/cpp) 题目背景 小小迪带你划水. 题目描述 原题 输入输出格式 输入格式: 第一行一个数 T. 接下来 T 行每行一个数表示 n 输出格式: 输出 T 行 ...
- iOS 根据时间戳计算聊天列表的时间(上午/下午)
把时间戳转成聊天时间(上午 10:00 . 昨天 14:00 . 3月15日 15:00) +(NSString*)ChatingTime:(NSString *)timestring{ int ...
- 免费开源的会计软件 GnuCash 3.4 发布
导读 GnuCash 3.4已经发布,GnuCash是免费和开源的会计软件.GnuCash开发团队宣布推出GnuCash 3.4,这是3.x稳定版系列的第五版. 变化 在3.3和3.4之间,完成了以下 ...
- P1140 相似基因 最长公共子序列
思路 类似于最长公共子序列 把一段基因和另外一段基因匹配 不够长的用空基因替换 #include<bits/stdc++.h> using namespace std; const in ...
- P1495 曹冲养猪
原题链接 https://www.luogu.org/problemnew/show/P1495 这个题明显的中国剩余定理(孙子定理),如果有不懂孙子定理的点这个链接https://baike.bai ...