
数据结构
playwfun
宁静致远
展开
-
腾讯九月6号红黑棋子顺序调整问题
记腾讯九月6号红黑棋子顺序调整问题。对于这样做转移的最优性质还需要继续思考。定义状态d[i][j]代表前i个黑子和前j个红子已经交换到了序列最前面且剩余棋子都按照原来顺序被交换到这i+j个棋子之后。那么d[i][j] = max(d[i+1][j] + sum(i,j,black[i+1] -1) , d[i][j+1] + sum(i,j,red[j+1] -1))sum(i,j,k)代表当前i个黑子和前j个红子已经交换到了序列最前面时,把剩余子中原来位置为K的子移动到i+j+1的位置,需原创 2020-09-08 11:22:41 · 324 阅读 · 0 评论 -
ZOJ 3826(字符串hash)
本体的意思是给了一个{ key:value , key:value ..... }的串,且value的形式可以也是这个形式(即是递归的),那么给定一些key1.key2.key3...类似如此的键值查询,让输出对饮键下的value,(没有该键值输出Error!)example: 输入 :{"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bo原创 2015-08-01 09:55:18 · 487 阅读 · 0 评论 -
HDU - 5324(分治+树状数组)
本题目的原来意思是,给定两个长度为n的数组,L,R,要求一个子序列(可以不连续)使的L递减,R递增。分析:加上下标递增,两个维度增,一个减,那么考虑 d[ i ]代表以i为起点的串往后找能得到的最大长度,用分治方法更新最有值。那么下面说一下,怎么分治维护最优。首先,数组原顺序保持不变,就是下标递增,对于l - > r区间的每个点的最优值,先求出右半区间每个点的最优值,然后把左右区间分原创 2015-07-29 21:07:26 · 1105 阅读 · 0 评论 -
ZOJ -- 2317(矩阵快速幂 + 大数简单处理)
题意:给定三个参数 b(b思路:从任意列状态出发,找到一个可以匹配的列状态,得到转移,那么如果一个状态成功转移b-1次,就是该问题的一个可行解。由于n很小,列状态最多32个,可以先构造转移矩阵,A(n,n)。其中a(i,j)代表i状态可以与j匹配。显然该矩阵为对称矩阵。那么A*A中每个元素代表的含义有是什么?显然每个点 b(i,j) 是有a的第i行和第j列相乘得来的,第i行原创 2015-05-21 12:51:15 · 377 阅读 · 0 评论 -
Codeforces Round #301 (Div. 2) -E(逆序对问题)
本题的意思是给出一个无限长的(1,2,3,4.....) 的序列,给出n对数,交换该以该对数为下标的位置的值。求交换完以后的逆序对。那么,由于数据范围为n考虑分开求被改变位置数的相对逆序 和 每个被改变位置数和未被改变位置数的逆序。那么,被改变位置地数,离散化一下,用树状数组,或者是归并排序求一下相对逆序对。而第二种逆序。则是对每个值,在最后被改动序列里有一个位置rank1原创 2015-05-05 17:52:07 · 468 阅读 · 0 评论 -
HDU - 3486(RMQ o(1)查询)
首先,要说明该题不满足二分性质。但是不用二分过不了该题啊,RMQ的o(1)查询,还有二分时上线设为n和n+1交上去结果不同,只能说明二分不满足单调性。#include #include #define max(a,b) a>b?a:b#define min(a,b) a<b?a:bconst int N=200105;int n,Q,c[N],a,b;int st[N][20]原创 2015-04-17 13:57:13 · 415 阅读 · 0 评论 -
山东省第四届省赛 H (划分树)
划分树能求区间第k小值。所以本题利用划分树+二分即可。#include #include #include using namespace std;#define N 100500#define MID ((l+r)>>1)struct tree{int s[N],t[20][N],num[20][N],n;void Build(int c,int l,int r){原创 2015-04-07 14:55:29 · 391 阅读 · 0 评论 -
UVA - 12219(唯一性标记法的应用)
题目就是用先建立#include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int maxn = 505555;struct node{ int c; int l,r; node(int原创 2015-03-14 08:25:55 · 1104 阅读 · 0 评论 -
UVA 1493 DRAW A MASS
第一个为线段树版本直接覆盖即可#include #include #include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int n,m;const int N = 211;const int M = 51111;i原创 2015-01-23 16:42:40 · 476 阅读 · 0 评论 -
uva - 1232
#include #include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define INF 1000001000const int maxn = 111111;int MAX[maxn<<2],col[maxn<<2]原创 2014-09-19 10:37:57 · 575 阅读 · 0 评论 -
UVA - 11997(优先队列)
#include #include #include #include using namespace std;typedef long long LL;const int maxn = 1000;struct Item{int s,b;Item(int s=0,int b=0):s(s),b(b){}bool operator <(const Item& rhs) cons原创 2014-10-15 18:25:36 · 582 阅读 · 0 评论 -
codeforces 319 div1 D(Matrices)
本题目的意思是给定了一个有向图,n个点,m条变,n解析:用一个矩阵A(1,n) 代表当前该人走了固定步数可以到达的点,0代表不可以到达,1反之。用一个B(n,n)代表当前的转移矩阵,即那些边是可以走的,那么把所有边按照d排序,那么在下一条边可以走之前,必须先由现在可达的状态至少再走差值步数,如果走了小于等于差值步数到达了n,那么只需在原来没走差值步的基础上枚举走几步可到达即可。为了原创 2015-09-14 11:12:59 · 4025 阅读 · 0 评论 -
UVA 11922(用splay实现可分裂和合并的序列)
介绍一下,splay保存的序列,序列不再是有序的,只是维持相对位置,splay主要实现对第k个位置的元素旋转到根,然后实现分裂与合并,要实现找到第k个元素,就必须维护一个s(以该节点为根的子树有多少节点)。对于本题目而言,要实现翻转,可以类比线段树来加惰性标志,实现翻转。#include #include #include #define rep1(i,x,y) for(int i=原创 2015-09-30 20:28:32 · 560 阅读 · 0 评论 -
codeforces 985F(hash)
首先本题目的意思是给定一个母串,然后每次查询两个长度相等的子串是否同构,同构的定义如下:For two given strings s and t, say S is the set of distinct characters of s and T is the set of distinct characters of t. The strings s and t are isomorphic...原创 2018-05-23 10:26:47 · 932 阅读 · 0 评论 -
(AC自动机 + 矩阵加速)
题意: 给定最多十个最长不超过10的病毒dna串,求长度为m(m 分析: 用自动机建立状态转移图,然后建立转移矩阵 定义初始状态为[1 , 0 , 0 , 0 ....]代表当前长度为0可转移到各个状态的方案数目,然后乘上m次转移矩阵即答案。#include #include #include #include #include using names原创 2016-04-08 09:50:00 · 433 阅读 · 0 评论 -
HDU 4123(RMQ(o1)模板 , 树直径)
题目的意思:给出N(n先求出每个点的最远到达距离,树直径简单求解方法。RMQ处理得到的最远距离数组,线性扫描求每个值#include#include#include#include#includeusing namespace std;typedef long long ll;#define lson l,m,rt<<1#define rson m+1,r原创 2016-04-05 20:51:31 · 352 阅读 · 0 评论 -
FZU 2082(树链剖分模板题)
一颗树边上的权在变动,动态求两点之间的和。#include #include #include #include #include #include #include #include #include using namespace std;#define LL long long#define pi acos(-1.0)//#pragma comment(linke原创 2016-04-05 20:06:27 · 343 阅读 · 0 评论 -
codeforces 589G(birnary serach + bit + 离线)
题意:很明显给定n,m(n,m并给定一个长度为m的数组 , 代表m天每个人可以工作多长时间。下面共有n对二元组,(d,t)代表第i个人需要完成工作量为t的任务,可是每天要么不干,要么先拿出d的时间来准备,然后剩下的可行时间工作,问每个人最少需要到第几天完成任务。分析:这样的题目,很明显的思路,离线排序使之产生顺序化,然后从一边开始考虑,是的前面兼顾后面的条件,加上点数据结构即可原创 2015-10-30 10:01:19 · 555 阅读 · 0 评论 -
codeforces 587C(树上的倍增算法)
分析:同LCA相似的思路,维护st[ i ][ j ] 代表从节点i往上(不包含i)走2^j个节点前十个最小值集合,这样一直倍增着往上走秩序logn次集合合并即可。#include #define fst first#define snd second#define ALL(a) a.begin(), a.end()#define clr(a, x) memset(a, x, s原创 2015-11-09 22:08:23 · 1338 阅读 · 0 评论 -
UVALive 3490(失配 + 解方程)
本题目的意思:给定n(n<= 26)用头1-n个大写字母来随机生成串,问要使串中有一个给定的子串(长度不超过12)平均期望的串长。可以考虑当前d[i]代表在当前随机生成的文本串与模板穿匹配到了i,生成给定的子串的期望长度。很明显d[i]=sum(d[f[i][k]])/n+1(1<=k<=n);这里的f[i][k]为下一个字符为k那么和模板串的最大匹配。若用自动机建立是失配边的话,原创 2015-10-10 16:04:42 · 468 阅读 · 0 评论 -
codeforces 593E(矩阵类题目)
给定一个R * C不超过20的图,一开始人在(1,1)然后给出之多100000个时间序列(按时间递增给出),每个时间可能发生的事情是在某个位置出现猫(有猫的地方人不能存在),猫在某个位置消失,询问人从1,1点在1时刻出发在t时刻可以走到(x , y)的所有可行性路径数目。分析:典型的矩阵转移题目。初始时用一个人d[ i ][ j]....代表人从1,1出发走了当前步数,可以走到1的原创 2015-11-09 17:45:32 · 560 阅读 · 0 评论 -
BZOJ 3991(dfs序 + LCA 公式推导)
题意:给定一颗带权的树(n分析:到达所有点并返回,是这些点用最少的边连起来的权值的2倍。这样只需动态计算,最少边的和。这里以1为根节点,跑一遍dfs序。考虑插入:那么如果插入点得dfs序,在集合中存在点dfs序的中间,找到比之小的最大点,和比之大的最小点,因为插入点在内部这两点连线与该点得距离最小。否则,插入点在外部,找dfs序最大和最小点,即可。删除是插入的逆操作。原创 2015-09-18 14:28:59 · 810 阅读 · 0 评论 -
UVA 1664(思路题目)
给定一颗树,边上有权值,让求从一点出发到其他所有点路径上最小权之和尽量打,输出最大值。思路: 考虑每个最小边权的边若获得了两边联通分量的最大值,那么该树的最大值也就递推了出来,这样直接递归求最大值,由于正向写系数较大,可以考虑倒过来,权值从大到小,然后用并查集合并即可。//#pragma comment(linker, "/STACK:1024000000,1024000000")#原创 2015-09-17 10:21:13 · 591 阅读 · 0 评论 -
UVA - 11402(线段树+区间离散)
本题查询区间最多1000个,可离散化(200)原创 2014-10-15 11:21:14 · 561 阅读 · 0 评论 -
UVA - 1354(指针结点的联合方法 || 高效算法复杂度未算出)
指针结点联合,即每个指针指向一个实际node,原创 2014-10-10 18:35:07 · 790 阅读 · 0 评论 -
UVA - 1401
本题很容易想到状态转移方程原创 2014-09-12 20:44:22 · 554 阅读 · 0 评论 -
HDU - 1542 (扫描线+线段树优化)
#include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m,r,rt<<1|1#define INF 1000000100const int maxn = 210;double ty[maxn];int kk;int find(double va原创 2014-09-23 10:42:04 · 617 阅读 · 0 评论 -
线段树 add操作(无惰性标记)
#include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,(rt<<1|1)#define INF 1000000100const int maxn = 1000010;int sum[maxn<<2],MIN[maxn<<2],MAX[m原创 2014-09-11 15:14:53 · 645 阅读 · 1 评论 -
uva - 11992 (下推法改进版: 1.246 秒)
#include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,(rt<<1|1)#define INF 1000000100const int maxn = 1000010;int sum[21][maxn<<2],MIN[21][maxn<<原创 2014-09-11 17:27:20 · 553 阅读 · 0 评论 -
线段树 add操作(惰性标记下推)
#include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,(rt<<1|1)#define INF 1000000100const int maxn = 1000010;int sum[maxn<<2],MIN[maxn<<2],MAX[m原创 2014-09-11 15:41:44 · 687 阅读 · 1 评论 -
hdu 1542 Atlantis (扫描线方法 o(n^2))
#include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define INF 1000000100const int maxn = 210;double ty[maxn],kk;int flag;struct Lin原创 2014-09-22 21:48:30 · 588 阅读 · 0 评论 -
UVA - 11992
#include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,(rt<<1|1)#define INF 1000000100const int maxn = 1000010;int sum[21][maxn<<2],MAX[21][maxn<<原创 2014-09-10 22:03:55 · 578 阅读 · 0 评论 -
POJ-3667 线段树(区间合并)
#include #include #include #include using namespace std;#define lson l,m,rt#define rson m+1,r,rt#define INF 1000001000const int maxn = 100100;int sum[maxnvoid build(int l,int r原创 2014-09-21 19:57:46 · 598 阅读 · 0 评论 -
POJ - 2528 线段树
#include #include #include #include #include using namespace std;#define lson l,m,rt#define rson m+1,r,rt#define INF 1000001000const int maxn = 51010;int cover[maxnvoid build原创 2014-09-19 11:38:53 · 508 阅读 · 0 评论 -
uva - 11525 Permutation
#include #include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 50110;int vis[maxn<<2];void build(int l,int r,int rt){vis[rt]=0;if原创 2014-09-26 11:11:34 · 435 阅读 · 0 评论 -
线段树 (矩形面积并&&周长并 - 来自notonlysuccess)
矩形面积并hdu1542 Atlantis题意:矩形面积并思路:浮点数先要离散化;然后把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用cnt表示该区间下边比上边多几个,sum代表该区间内被覆盖的线段的长度总和这里线段树的一个结点并非是线段的一个端点,而是该端点和下一个端点间的线段,所以题目中r+1,r-1的地方可以自己好好的琢磨一下线段树操作:update转载 2014-09-24 14:44:31 · 592 阅读 · 0 评论 -
矩阵快速幂模板
struct Matrix{ int mat[maxn][maxn];};int n, k;Matrix Matrix_mul(Matrix a, Matrix b){ Matrix ret; memset(ret.mat, 0, sizeof(ret.mat)); for(int i = 1; i <= k; i++) for(int j = 1原创 2014-11-13 15:40:01 · 488 阅读 · 0 评论 -
HDU - 4973(线段树+二分)
#include #include #include using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long LL;const int maxn = 51000;int a[maxn];long long Pow(int x,int y){ return a[y];原创 2014-11-13 13:31:59 · 600 阅读 · 0 评论 -
POJ - 1741(树分治)
朴素算法枚举两点为o(n^2) ,若用原创 2014-09-30 14:46:18 · 538 阅读 · 0 评论 -
trie树的 表示方法和实现
#include#include #include#include#include #include using namespace std;#define INF 1000000; //引用只是纯粹的一个变量的别名,至于怎么做到的和指针无关,和语言设计方法有关,待深究;typedef struct node{char c;node *first原创 2014-10-24 12:02:01 · 595 阅读 · 0 评论