数据结构和算法
海将河推走
这个作者很懒,什么都没留下…
展开
-
稀疏表
文章目录稀疏表构造稀疏表查询范围最小值洛谷P3865 ST表稀疏表学习稀疏表:https://www.youtube.com/watch?v=c5O7E_PDO4U稀疏表(Sparse Table),也叫ST表,用来解决范围最值查询(Range Minimum Query)问题。假设有一个数组,我们需要对该数组执行如下操作(以下全部假设数组下标从0开始):求一个区间[low,high][low,high][low,high]内的最大值不同的解决方法所花的时间不同:解决方法预处理时原创 2020-10-05 15:00:06 · 966 阅读 · 0 评论 -
线段树
文章目录线段树构造线段树使用普通方式求范围和懒惰传播(lazy propagation)使用懒惰传播的方式更新范围值使用懒惰传播的方式求范围和洛谷P3372 线段树1线段树学习线段树(segment tree):https://www.youtube.com/watch?v=ZBHKZF5w4YU学习线段树的懒惰传播(lazy propagation):https://www.youtube.com/watch?v=xuoQdt5pHj0首先简单介绍一下什么是线段树,以及为什么要使用线段树。假设有原创 2020-10-04 13:10:50 · 295 阅读 · 0 评论 -
洛谷P3368 树状数组2
文章目录洛谷P3368 树状数组2题目链接树状数组差分数组解决洛谷P3368 树状数组2题目链接https://www.luogu.com.cn/problem/P3368树状数组学习树状数组:https://www.youtube.com/watch?v=v_wj_mOAlig首先简单介绍一下树状数组,以及为什么要使用树状数组。假设有一个数组,我们需要对该数组执行如下操作(以下全部假设数组下标从1开始):求前i项的和。将第i项元素加上某个给定的值。如果使用普通的数组,执行第一个操作原创 2020-10-03 20:15:14 · 293 阅读 · 0 评论 -
用于不相交集合的数据结构(并查集)
Disjoint_set.h#pragma oncetemplate <typename _Ty>struct Node { Node(const _Ty& d) : data(d) {} _Ty data; int rank = 0; Node<_Ty>* p = nullptr;};template <typename _Ty&...原创 2019-11-07 23:41:18 · 175 阅读 · 0 评论 -
gcd和lcm算法
gcd和lcm算法gcd(greatest common divisor)为最大公约数,lcm(least common multiple)为最小公倍数。gcd算法下面的算法是欧几里得算法(Euclidean algorithm),也叫辗转相除法。int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b);}lcm算法两个整数的最小公倍数与最大公因数之间有如下的关系:原创 2020-11-11 19:57:21 · 8609 阅读 · 0 评论 -
选择算法(顺序统计量)
代码如下,具体思路请参考《算法导论》#include <iterator>template <typename RanIt>inline RanIt partition(RanIt first, RanIt last) { if (first == last) return first; --last; // pivot RanIt it = first;...原创 2020-04-04 23:22:42 · 1355 阅读 · 0 评论 -
C++标准库全排列算法
下面是gnu c++的源码template<typename _BidirectionalIterator, typename _Compare>bool__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) { if (__fir...原创 2019-11-06 17:27:17 · 258 阅读 · 0 评论 -
C++插入排序算法
#include <iostream>void insertion_sort(int arr[], int length){ for (int i = 1; i < length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 &&...原创 2019-10-24 21:00:07 · 89 阅读 · 0 评论 -
C++堆排序算法
#include <iostream>void __max_heap(int arr[], int p, int legth){ int child = 0; while (p * 2 + 1 < legth) { child = p * 2 + 1; if (child + 1 < legth &&...原创 2019-10-24 20:59:32 · 131 阅读 · 0 评论 -
C++快速排序算法
#include <iostream>int __partition(int arr[], int length){ int i = -1; int j = 0; int mid = arr[length - 1]; for (; j < length - 1; j++) { if (arr[j] <= mid) {...原创 2019-10-24 20:58:43 · 123 阅读 · 0 评论 -
C++选择排序算法
#include <iostream>void select_sort(int arr[], int length){ for (int i = 0; i < length - 1; i++) { for (int j = i + 1; j < length; j++) { int min_index = i; ...原创 2019-10-24 21:04:57 · 111 阅读 · 0 评论 -
C++冒泡排序算法
代码如下#include <iostream>void bubble_sort(int arr[], int length){ for (int i = 0; i < length - 1; i++) { for (int j = i + 1; j < length; j++) { if (arr[j] < ...原创 2019-10-24 21:04:07 · 558 阅读 · 0 评论 -
C++计数排序算法
代码如下计数排序和输入数据相关,因此代码意义不大#include <iostream>#include <array>using std::array;using std::cout;using std::endl;template <typename _Ty, size_t _N>void count_sort(const array...原创 2019-10-24 21:02:04 · 219 阅读 · 0 评论 -
C++希尔排序算法
#include <iostream>void __insertion_sort(int arr[], int i, int interval){ int key = arr[i]; int j = i - interval; while (j >= 0 && arr[j] > key) { arr[j + ...原创 2019-10-24 21:00:52 · 160 阅读 · 0 评论 -
C++归并排序算法
代码如下#include <iostream>#include <cstddef>void __merge(int arr[], int begin, int mid, int end){ int n1 = mid - begin; int n2 = end - mid; int* A = new int[n1 + 1]{}; ...原创 2019-10-24 20:58:00 · 142 阅读 · 0 评论 -
C语言汉诺塔(Hanoi)算法
#include <stdio.h>void Hanoi(int n, char A, char B, char C) { if(n==1) { printf("%c->%c\n", A, C); } else { Hanoi(n-1, A, C, B); printf("%c->%c\n", A, C); Hanoi(n-1, B, A, C);...原创 2018-11-17 20:44:03 · 705 阅读 · 0 评论 -
C语言判断回文序列函数
int fun(char *p) { int i=0, j=0; j = strlen(p) - 1; while(p[i]==p[j] && i<=j) { i++; j--; } if(i>j) { return 1;//此时为回文序列 } else { ...原创 2019-01-20 21:10:46 · 2901 阅读 · 1 评论