随笔分类 -  算法-模版

分块分段
摘要:简介:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 各类乱七八糟数据结构的本质:定义若干正则集合,并将他们组织成某种合适的结构,而查找算法就是要把查找的结果表示成若干个正则集合的划分,进而在每个正则集合中通过枚举的方式实现查找。 分块其实就是一种最简单的组织形式——hash 阅读全文

posted @ 2016-02-20 14:56 蓦辰 阅读(680) 评论(0) 推荐(0)

数论-毕达哥拉斯三元组
摘要:方程形式: X^2 + Y^2 = Z^2X,Y,Z即勾股定理的三条直角边。本元毕达哥拉斯的解即三边互质的一组解,如(3,4,5),其求法如下:假定m,n为任意正整数 (m>n) ,满足 gcd(m,n) = 1且m%2 != n%2则:X=m^2-n^2;Y=2*m*n;Z=m^2+n^2;求得本... 阅读全文

posted @ 2016-01-26 14:14 蓦辰 阅读(632) 评论(0) 推荐(0)

几何模版-凸包
摘要:Graham算法平均复杂度:N log(N)#include #include #include #include #include using namespace std;const int Max = 1100;#define PI 3.1415926struct Point{ int x... 阅读全文

posted @ 2016-01-25 12:48 蓦辰 阅读(164) 评论(0) 推荐(0)

几何模版-多维最远曼哈顿距离
摘要:/*题意:给定N个D维坐标。求最远曼哈顿距离。解法:对于两个二维坐标A(xi,yi),B(xj,yj).其曼哈顿距离可能为以下四种情况:(xi-xj)+(yi-yj), (xj-xi)+(yi-yj), (xi-xj)+(yj-yi), (xj-xi)+(yj-yi)将坐标参数按点来分配之后变形如下... 阅读全文

posted @ 2016-01-25 11:12 蓦辰 阅读(1211) 评论(0) 推荐(0)

BFS-修改范例-开门系列-单扇多类门配多钥匙
摘要:/*BFS:多类单个门,配多把钥匙*/#include #include #include #include #include #include #include #include #include #include using namespace std;#includeconst int INF... 阅读全文

posted @ 2016-01-25 11:09 蓦辰 阅读(209) 评论(0) 推荐(0)

BFS-修改范例-状态压缩
摘要:/*BFS:二进制状态压缩*/#include #include #include #include #include #include #include #include #include #include using namespace std;#define MAXN 11111#includ... 阅读全文

posted @ 2016-01-25 11:08 蓦辰 阅读(214) 评论(0) 推荐(0)

BFS-修改范例-重点访问
摘要:/*BFS:重复访问类关键:对每个点的访问与否取决于到达该点的条件的优劣(本题关键在于访问该点是能量的多少,比原先多的即可走)*/#include #include #include #include #include #include #include #include #include #inc... 阅读全文

posted @ 2016-01-25 11:07 蓦辰 阅读(225) 评论(0) 推荐(0)

BFS-基础模版
摘要:/* 小水题当个模版*/#include #include #include #include #include #include using namespace std;#include #include #include #include #include #define M 1000 ... 阅读全文

posted @ 2016-01-25 11:06 蓦辰 阅读(159) 评论(0) 推荐(0)

字符串-最长回文子串O(n)
摘要:#include#include#include#include#includeusing namespace std;const int MAXN=110010;char s[2*MAXN];//MAXN太大只能放外面int p[MAXN*2];//求给定字符串str的最长回文子串int solv... 阅读全文

posted @ 2016-01-25 11:04 蓦辰 阅读(243) 评论(0) 推荐(0)

图论-最短路模版
摘要:Dijkstra算法-邻接矩阵形式复杂度:N^2/** Dijkstra算法,邻接矩阵形式:* 复杂度为O(n^2),仅可处理非负权图* cost: 任意两点边权(不相连边需赋值INF)* n: 图中点的总数(当前点从0开始编号)* lowcost:任意点到源点的最短路* beg:... 阅读全文

posted @ 2016-01-25 11:02 蓦辰 阅读(204) 评论(0) 推荐(0)

图论-最小生成树模版
摘要:Kruskal 算法复杂度:E log(2E)int F[MAXN];//并查集使用struct Edge{ int u,v,w;}edge[MAXM];//存储边的信息,包括起点/终点/权值int tol;//边数,加边前赋值为0void addedge(int u,int v,int w)... 阅读全文

posted @ 2016-01-25 10:55 蓦辰 阅读(166) 评论(0) 推荐(0)

图论-并查集模版
摘要:#includeint father[1005];int find(int x){ int r=x; while(father[r]!=r) {//沿着关系一直向上摸索 r=father[r]; } int i=x,k; //压缩 while(... 阅读全文

posted @ 2016-01-25 10:53 蓦辰 阅读(137) 评论(0) 推荐(0)

数论-矩阵快速幂模版
摘要:#include #include #include #include #include #include using namespace std;#define M 20int num, mod;//定义矩阵结构体struct mat{ int at[M][M];};struct mat d... 阅读全文

posted @ 2016-01-25 10:50 蓦辰 阅读(139) 评论(0) 推荐(0)

数论-GCD && 欧拉函数 && 快速求幂
摘要:int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b);}int Euler(int n){ //给定n,返回1~~(n-1)中与n互质的数的个数 //复杂度nlogn int i; int result;... 阅读全文

posted @ 2016-01-25 10:49 蓦辰 阅读(154) 评论(0) 推荐(0)

数论-O(N)打印素数表
摘要:#include#include#include#include#includeusing namespace std;#define MAXPRME 100000bool visit[MAXPRME+100];int prime[MAXPRME];//给定n,求所有>n) { ... 阅读全文

posted @ 2016-01-25 10:47 蓦辰 阅读(200) 评论(0) 推荐(0)

划分树-修改范例
摘要:/*HDU 3473 Minimum Sum求询问区间内 所有数与中位数的差的绝对值的和*/#include#include#include#includeusing namespace std;const int MAXN=200010;int tree[20][MAXN];int sorted... 阅读全文

posted @ 2016-01-25 10:46 蓦辰 阅读(231) 评论(0) 推荐(0)

划分树-模版(求区间第K大)
摘要://复杂度log(n)#include #include #include #include #include #include #include #include #include #include #include #includeusing namespace std;#include con... 阅读全文

posted @ 2016-01-25 10:44 蓦辰 阅读(268) 评论(0) 推荐(0)

输入挂
摘要:简化版:(正int输入挂)int read(){ char ch=' '; int ans=0; while(ch'9') ch=getchar(); while(ch='0') { ans=ans*10+ch-'0'; ch=... 阅读全文

posted @ 2016-01-25 10:43 蓦辰 阅读(156) 评论(0) 推荐(0)

线段树-区间查询模版
摘要:/**模版原题:PKU 3468*线段树*区间求和*/#include #include using namespace std;#define lson l , m , rt > 1)); // sum[rt*2]+=( add值*其旗下最终子节点个数); sum... 阅读全文

posted @ 2016-01-25 10:35 蓦辰 阅读(222) 评论(0) 推荐(0)

线段树-单点更新模版
摘要:/***模版原题:HDU 1754*线段树-单点加减*区间求最值*/#include #define lson l , m , rt > 1; //m=(l+r)/2; build(lson); build(rson); PushUP(rt); //回溯更新父节点}//单点修... 阅读全文

posted @ 2016-01-25 10:34 蓦辰 阅读(127) 评论(0) 推荐(0)