NOIp 数据结构专题总结 (1):STL、堆、并查集、ST表、Hash表
2024-09-03 01:02:11
系列索引:
STL structure
STL 在 OI 中的运用:https://oi.men.ci/stl-in-oi/
std::vector
#include <vector>
std::vector<type> v;
v.push_back(x);
v.pop_back();
int *a = new int[N];
std::bitset
#include <bitset>
std::bitset<size> bs;
神似 bool
数组。
SAO操作:
std::bitset<N> g[N]; // store a graph, O(n^2/32)
std::map
#include <map>
std::map<typeA, typeB> mp;
mp[x] = y;
map<int,bool> g[n];
g[x][y] = true;
map<int,bool>::iterator it;
for (it=g[x].begin(); it!=g[x].end(); ++it)
it->first, it->second
std::queue
#include <queue>
std::queue<type> q;
q.push(x);
q.pop();
x = q.front();
std::stack
#include <stack>
std::stack<type> s;
s.push(x);
s.pop();
x = s.top();
std::deque
#include <queue> / #include <deque>
std::deque<type> dq;
std::priority_queue
#include <queue>
std::priority_queue<type> pq; // default: greater first
std::priority_queue<int, std::vector<int>, std::greater<int> > pq; // lower first
struct type { friend bool operator < (type a, type b) {return ...;} };
struct cmp { bool operator() (type a, type b) {return ...;} };
std::priority_queue<type, std::vector<type>, cmp> pq;
std::set
#include <set>
std::set<type> st; // 会自动去重
std::multiset<type> st2; // 不去重
时间复杂度每次操作 \(O(\log{n})\)。
堆 Heap
Pure
复杂度均摊 \(O(\log{n})\).
void put(int x) {
int now = ++heap_size, next;
heap[heap_size] = x;
while (now > 1) {
next = now >> 1;
if (heap[now] >= heap[next]) break; // lower root
swap(heap[now], heap[next]);
now = next;
}
}
int get(int x) {
int now = 1, next, res = heap[1];
heap[1] = heap[heap_size--];
while ((now << 1) <= heap_size) {
next = now << 1;
if (next < heap_size && heap[next+1] < heap[next]) next++;
swap(heap[now], heap[next]);
}
return res;
}
堆的初始化:新建一个空堆,把这些元素依次加进堆,时间 \(O(n\log{n})\)。如果把这些元素 random_shuffle 一下再加进来,期望时间 \(O(n)\)。可以先随意把这些元素放进完全二叉树,再考虑从底向上令每个子树满足堆性质。
堆的删除:因为我们只关心堆顶的元素,我们可以把被删掉的元素暂时留在堆中,并保证当前堆顶的元素未被删除即可。一种实现方法是再开一个堆存储被删除但留在原堆中的元素,一旦两个堆堆顶相等,就一起弹出堆顶。总时间复杂度 \(
最新文章
- KAFKA一异常处理记录
- python高级之操作数据库
- 钩子机制(hook)
- echart.js的使用与API
- Android优化——UI优化(四) 使用stytle
- Spring核心框架 - AOP的原理及源码解析
- 关于web开发的一点理解
- 30分钟Git命令入门到放弃
- gitHub项目框架使用排名
- 如何清理多余的Windows桌面右键菜单
- android中用get和post方式向服务器提交请求
- Gazebo與ROS版本說明
- this()基础用法
- selenium控制浏览器
- 走进JDK(六)------ArrayList
- 【转】 Apk文件及其编译过程
- 18-Python3 迭代器与生成器
- RabbitMQ入门介绍
- js把一个数组插入到另一个数组的指定位置
- centos7.2 安装 nginx
热门文章
- 一个有关Golang Deferred Function 执行顺序的问题
- Krustal重构树
- Far and away the best prize that life has given to us is the chance to work hard at work worth doing
- 5G调研与总结
- Linux apt-get命令的基本使用
- Mac--PHP已经开启gd扩展验证码不显示
- 20191105 《Spring5高级编程》笔记-第16章
- 模块内高内聚?模块间低耦合?MVC+EF演示给你看!
- python基础-2 编码转换 pycharm 配置 运算符 基本数据类型int str list tupple dict for循环 enumerate序列方法 range和xrange
- vue—两个数组,去重相同项