今天整理最近的考试题

发现一个东西叫做优先队列

priority_queue(说白了就是大根堆)

但是

我对堆的了解还是很少的

所以

我决定手写一个堆

于是我写了一个简单的堆

手写的堆说白了就是个二叉树

能不更新维护的二叉树,每次维护的时间为logn

但是各种查询(empty,top什么的)时间为1;

感觉这个堆还写的像模像样

于是兴冲冲的去ac特别简单题合并果子

没a;

改了好久才ac

现在贴一下这个代码

来,上代码:

#include<cstdio>

using namespace std;

int heap[],num,jkl,cur,n,ans=;

char ch;

void swap(int &a,int &b)//交换函数
{
int t=a;
a=b,b=t;
} void qread(int &x)//读入优化
{
x=,jkl=;ch=getchar();
while(ch>''||ch<''){if(ch=='-') jkl=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+(int)(ch-'');ch=getchar();}
x*=jkl;
} void heap_up(int now)//向上维护(添加元素时)
{
if(now==) return;
int cnm=now/;
if(heap[cnm]>heap[now])
{
swap(heap[cnm],heap[now]);
heap_up(cnm);
}
} void heap_push(int kol)//添加元素同时维护
{
num++;
heap[num]=kol;
if(num==) return ;
heap_up(num);
} void heap_down(int now)//向下维护(删除元素时)
{
int l,r;
if(now*+<=num)
{
l=now*,r=now*+;
if(heap[l]<heap[now])
{
if(heap[r]<heap[l])
{
swap(heap[r],heap[now]);
heap_down(r);
}
else
{
swap(heap[l],heap[now]);
heap_down(l);
}
}
else
{
if(heap[r]<heap[now])
{
swap(heap[r],heap[now]);
heap_down(r);
}
}
}
else
{
if(now*<=num)
{
l=now*;
if(heap[l]<heap[now]) swap(heap[l],heap[now]);
}
}
} void heap_pop()//删除元素
{
heap[]=heap[num];
num--;
heap_down();
} int heap_top(){return heap[];}//查询堆顶元素 bool heap_empty(){return num==?true:false;}//查询是否为空 int main()
{
qread(n);
for(int i=;i<=n;i++)
{
qread(cur);
heap_push(cur);
}
for(int i=;i<n;i++)
{
cur=heap_top();
heap_pop();
cur+=heap_top();
heap_pop();
heap_push(cur);
ans+=cur;
}
printf("%d\n",ans);
return ;
}

AC日记——手写堆ac合并果子(傻子)的更多相关文章

  1. AC日记——二叉堆练习3 codevs 3110

    3110 二叉堆练习3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给定N(N≤500,000)和N个整 ...

  2. Luogu [P1334] 瑞瑞的木板(手写堆)

    其实这个题完全不需要用手写堆,只需要一遍遍sort就行了…… 但是! 为了练习手写堆,还是用手写堆做了. 在做本题之前,如果你没有什么思路的话,建议先做Luogu的合并果子. 好,假设你已经做过了合并 ...

  3. spfa+剪枝 或者 dij+手写堆+剪枝 UOJ 111

    http://uoj.ac/problem/111 好像NOIP里面的题目...有好多都是...能通过xjbg剪枝来...AC题目的? 得好好学一下这些剪枝黑科技了... 思路:我觉得这位大佬说的很完 ...

  4. 手写堆优化dijkstra

    \(dijkstra\) 算法的堆优化,时间复杂度为\(O(n+m)\log n\) 添加数组\(id[]\)记录某节点在堆中的位置,可以避免重复入堆从而减小常数 而这一方法需要依托手写堆 #incl ...

  5. [转载] 管Q某犇借的手写堆

    跟gxy大神还有yzh大神学了学手写的堆,应该比stl的优先队列快很多. 其实就是维护了一个二叉堆,写进结构体里,就没啥了... 据说达哥去年NOIP靠这个暴力多骗了分 合并果子... templat ...

  6. hihoCoder 第136周 优化延迟(二分答案+手写堆)

    题目1 : 优化延迟 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho编写了一个处理数据包的程序.程序的输入是一个包含N个数据包的序列.每个数据包根据其重要程度不同 ...

  7. BZOJ 1975 魔法猪学院(A*+手写堆)

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 1941  Solved: 595 [Submit][Sta ...

  8. 手写堆_C++

    一般主程序中拿堆顶元素 x=h[]; h[]=h[top--]; down(); 在堆尾加入元素 h[++top]=x; up(top); 上浮下沉操作 inline void up(int x) { ...

  9. 手写堆的dijkstra

    颓废.. #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...

随机推荐

  1. jsoup html采集器

    package com.forex.collect; import java.io.IOException;import java.util.HashMap;import java.util.Iter ...

  2. Visual Studio 2013下JSON可视化工具

          Visual Studio 2013现在我们有个小工具可以实现JSON可视化,这样给我们调试JSON提供了便利. JSON这种数据格式已经比较流行,在WEB前端随处可见. 在你需要安装VS ...

  3. Web网站数据”实时”更新设计

    请注意这个实时打上了双引号,没有绝对的实时,只是时间的颗粒不一样罢了(1ms,1s,1m). 服务器数据有更新可以快速通知客户端.Web 基于取得模式,而服务器建立大量的和客户端连接来提供数据实时更新 ...

  4. Android 手机卫士14--Widget窗口小部件AppWidgetProvider

    1.AndroidManifest.xml根据窗体小部件广播接受者关键字android.appwidget.action.APPWIDGET_UPDATE 搜索android:resource=&qu ...

  5. 结合微软开放api,使用MSN,Hotmail等登陆Sharepoint网站

    成功使用Windows Live账号登陆SharePoint系统. 附上创建SPTrustedIdentityTokenIssuer的PS脚本====================RegSTS.ps ...

  6. [android] 短信发送器

    /*****************2016年4月23日 更新********************************/ 知乎:什么是 7 位元的字符? 英文字符难道不是 8 bit 是一个字 ...

  7. Create a “% Complete” Progress Bar with JS Link in SharePoint 2013

    Create a “% Complete” Progress Bar with JS Link in SharePoint 2013 SharePoint 2013 has a lot new fea ...

  8. android 类ios actionsheet效果

    1.http://blog.csdn.net/zhaoxy_thu/article/details/17733389 2. https://github.com/ojhariddhish/action ...

  9. Objective-C之代理设计模式小实例

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  10. Regex Expression的资料和笔记整理

    维基百科:http://en.wikipedia.org/wiki/Regular_expression 正则表达式在线测试:http://tool.chinaz.com/regex/ 正则表达式,常 ...