堆
2017-01-21 20:27
387 查看
堆
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; int heap[101]; int heapSize; void put(int); void get(); int main() { int x;cin>>x; put(x); return 0; } void put(int d) { int now,next; heap[++heapSize] = d; now = heapSize; while(now > 1) { next=now >> 1; if(heap[now] >= heap[next])break; swap(heap[now],heap[next]); now = next; } } void get() { int now,next,res; res = heap[1]; heap[1]=heap[heapSize--]; now = 1; while(now * 2 <= heapSize) { next = now * 2; if(next < heapSize && heap[next+1] < heap[next])next++; if(heap[now] <= heap[next]) break; swap(heap[now],heap[next]); now = next; } return res; }
相关文章推荐
- InfoSYS-20170114
- 设计模式的原则
- 开发板linux中运行简单hello world应用程序的printf无输出的原因
- 开发板linux中运行简单hello world应用程序的printf无输出的原因
- 开发板linux中运行简单hello world应用程序的printf无输出的原因
- 开发板linux中运行简单hello world应用程序的printf无输出的原因
- 开发板linux中运行简单hello world应用程序的printf无输出的原因
- 开发板linux中运行简单hello world应用程序的printf无输出的原因
- priority_queue优先级重载
- 线段树
- OpenWrt 中安装配置Transmission
- python应用 (1) 解析xml
- iOS动画案例(1) 类似于qq账号信息里的一个动画
- 【设计模式】--代理模式 (Proxy模式)
- 【清明】并查集
- struts基本概念(2)
- More Effective C++学习笔记(4)-效率
- C#与C++在语法上的区别
- HTML5移动端手机网站开发流程
- 12 共享内存2