随笔分类 -  DS

摘要:昨晚脑子发涨,代码写的跟坨屎的,到底没过,今天早晨来看了看,查了几个bug过了。。。纯模拟+LCA找最近公共祖先,需要注意的几个地方:1、出处顺序,可以用dfs输出,不过每个dfs里面要开一个vector<>,然后排序。2、b操作可能出现询问Mr.X,Mr.X的brother个数为13、c操作时LCA不能是他两个本身。可以开一个pre,如果是他俩中的一个,再往前找一个就可以了。200+行的渣代码:View Code 1 //#pragma comment(linker,"/STACK:327680000,327680000") 2 #include <io 阅读全文
posted @ 2012-09-26 08:29 AC_Von 阅读(394) 评论(0) 推荐(0)
摘要:很早看过这个东西,不过今天遇到一道题发现居然没有写笔记。。。这里补上吧。以下是我从某个题的解题报告翻出来的。转载的别人的。。。。以此为例:对于N=8,K=3,8个元素序列1 3 -1 -3 5 3 6 7,窗口大小为3,也就是要求出(1, 3, -1), (3, -1, -3), (-1, -3, 5), (-3, 5, 3), (5, 3, 6), (3, 6, 7)这6个序列中的最小值,结果简单,就是-1, -3, -3, -3, 3, 3.使用单调队列,首先要有一个数据结构structnode {intseq, val; }用于记录队列中的元素及其在输入序列中的顺序。队列的状态是这样维护 阅读全文
posted @ 2012-07-25 15:44 AC_Von 阅读(298) 评论(0) 推荐(0)
摘要:http://www.hhanger.com/?p=134http://www.coderplusplus.com/?p=393POJ 2201const int N = 50010;int stack[N];int pre[N], lson[N], rson[N];struct node { int key; int val; int id;} st[N];bool operator < (const node& a, const node& b) { return a.key < b.key;}void init(int n) { memset(stack, . 阅读全文
posted @ 2012-07-23 19:38 AC_Von 阅读(589) 评论(0) 推荐(0)
摘要:数据结构trie图的建立和应用,DFAhdd2222,poj1204, poj2778, poj3691LCA和RMQ问题poj1330双端队列和它的应用poj2823(单调队列)左偏树poj3666,poj3016后缀树,后缀数组poj3415,poj3294, poj2774poj2758trie图的建立和应用,DFAhdu2222很裸的题,可以作为模板:View Code #include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cst 阅读全文
posted @ 2012-07-19 09:21 AC_Von 阅读(1152) 评论(0) 推荐(0)
摘要:详见:随机平衡二叉查找树Treap的分析与应用#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#define CL(arr, val) memset(arr, val, sizeof(arr))#define REP(i, n) for(i = 0; i < n; ++i)#define FOR(i, l, h) for(i = l; i <= h; ++i)#define FORD(i, 阅读全文
posted @ 2012-05-02 17:10 AC_Von 阅读(541) 评论(0) 推荐(0)
摘要:数据结构线段树poj2528,poj2828,poj2777,poj2886,poj2750, poj2892(区间更新)静态二叉检索树,平衡树treap,splaypoj2482, poj3468树状树组poj1195,poj3321, poj2352RMQpoj3264,poj3368并查集的高级应用poj1703, poj2492KMP算法poj1961,poj2406 线段树poj 2528以前做过多次,思路很明显,线段数分区间染色,最后统计颜色的个数。我犯了一个极度二百五的错误,导致这题提交5次!!!col的初始化是-1,更新时我当成0了。T_TView Code #inclu... 阅读全文
posted @ 2012-04-29 13:33 AC_Von 阅读(973) 评论(0) 推荐(0)
摘要:优先队列,据说标程是并查集,没思路。貌似优先队列都是直接用stl写的,又逼我用stl了。prioriry_queue不熟。ps: value值越小,优先级越高。所以重载 < 运算符时按优先级从大到小排序bool operator < (const node a, const node b) { if(a.day != b.day) return a.day > b.day; return a.type > b.type;}参考网上的代码View Code 1 #include <iostream> 2 #include <cstdio> 3 #i 阅读全文
posted @ 2012-04-01 21:11 AC_Von 阅读(414) 评论(0) 推荐(0)
摘要:题意看了老半天,就是说给一个序列[1, n],看是否是(1, 2, 3, ... n),入栈以后出栈时可以得到的序列。My Code:View Code #include <iostream>#include <cstdio>using namespace std;const int N = 100007;int st1[N], st2[N];int main() { //freopen("data.in", "r", stdin); int i, top1, top2, n; while(cin >> n) { fo 阅读全文
posted @ 2011-12-03 19:31 AC_Von 阅读(353) 评论(0) 推荐(0)
摘要:很裸的单调队列问题,不过O(n)的算法写出来5188+ms,超5s了。。。谁能告诉我500+ms的神级代码是什么。。.T_TMy Code:#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 1000005;struct node { int i; int num;}q[N];int ans[N];int b[N];int main() { //freopen("data.in", "r", st 阅读全文
posted @ 2011-11-20 20:39 AC_Von 阅读(211) 评论(0) 推荐(0)
摘要:话说单调队列!= 优先队列。可怜我捧着算导看了半天优先队列。题意读的很费劲,最后问得师兄。就是给定一个M,然后再给一个序列,求给出的序列里连续M个数中的最大值。最后把这些最大值输出,其实就是很裸的单调队列。然后开始在网上搜有关单调队列的资料,从这里http://www.felix021.com/blog/read.php?1965学会的。其实就是维持队列的单调性,别管是单调增还是单调减。队头元素永远是最列的最小值(或最大值)。 #include <iostream>#include <cstdio>#include <cstring>using namesp 阅读全文
posted @ 2011-11-20 17:34 AC_Von 阅读(218) 评论(0) 推荐(0)
摘要:/*我太二了, 上来就暴力,然后TLE两次。然后郁闷的去吃饭,回来一想栈的结构,10分钟AC。有种想撞墙的冲动!*///My Code:#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 200005;char s[N];char str[N];int main() { //freopen("data.in", "r", stdin); int len, top, i; while(cin.get 阅读全文
posted @ 2011-11-18 19:22 AC_Von 阅读(269) 评论(0) 推荐(0)
摘要:/*用到了class, template,析构函数,构造函数,C++菜鸟一只,大牛无视。各种bug,待修改。。。*///My Code:#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;const int N = 256;char map[N][N];template <class T>class Node{public: T data; Node * next; Node(const T &e, 阅读全文
posted @ 2011-10-17 22:05 AC_Von 阅读(811) 评论(0) 推荐(0)
摘要:大牛无视,小白代码。。。又是无聊的上机课......#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;typedef struct node{ int data; struct node * next;}node, *linklist;//顺序建立链表linklist creat(linklist &l , int n){ l = (linklist)malloc(sizeof(node)); linklist p, tail; tail = l; fo 阅读全文
posted @ 2011-09-13 15:47 AC_Von 阅读(231) 评论(0) 推荐(0)
摘要:数据结构上机课,无聊。。。(严蔚敏那本书上的例题)/*Another: VonData: 2011/09/06*/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;const int LIST_SIZE = 100;const int LISTINCREMENT = 10;struct sqlist{ int * elem; int len; int listsize;};//creatint InitList_s 阅读全文
posted @ 2011-09-06 16:05 AC_Von 阅读(185) 评论(0) 推荐(0)
摘要:Dijkstra算法 Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法能得出最短路径的最优解,但由于它遍历计算的节点很多,所以效率低。 Dijkstra算法是很有代表性的最短路算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹学等等。其基本思想是,设置顶点集合S并不断地作贪心选择来扩充这个集合。一个顶点属于集合S当且仅当从源到该顶点的最短路径长度已知。初始时,S中仅含有源。设u是G的某一个顶点,把从源到u且中间只经过S中顶点的路称为从源到u的 阅读全文
posted @ 2011-08-02 22:10 AC_Von 阅读(617) 评论(0) 推荐(0)
摘要:大家都知道,Trie树(又称字典树)是一种树型数据结构,用于保存大量的字符串。它的优点是:利用字符串的公共前缀来节约存储空间。相对来说,Trie树是一种比较简单的数据结构,比较易于理解。话说上帝是公平的,简单的东西是要付出相应的代价的!Trie树也有它的缺点,它的内存消耗非常大。下面介绍一个减小内存消耗的小技巧。 我们先看看这个题目:http://acm.sdut.edu.cn/judgeonline/showproblem?problem_id=1500 看看这段程序:#include <stdio.h>#include <string.h>#include < 阅读全文
posted @ 2011-07-31 18:35 AC_Von 阅读(2606) 评论(2) 推荐(1)
摘要:使用并查集查找时,如果查找次数很多,那么使用朴素版的查找方式肯定要超时。比如,有一百万个元素,每次都从第一百万个开始找,这样一次运算就是10^6,如果程序要求查找个一千万次,这样下来就是10^13,肯定要出问题的。 这是朴素查找的代码,适合数据量不大的情况:int findx(int x){ int r=x; while(parent[r] !=r) r=parent[r]; return r;} 下面是采用路径压缩的方法查找元素:int find(int x) //查找x元素所在的集合,回溯时压缩路径{ if (x != parent[x]) { parent[x] = find(pare. 阅读全文
posted @ 2011-07-31 16:02 AC_Von 阅读(5737) 评论(1) 推荐(4)
摘要:转载自大牛Matrix67的博客http://www.matrix67.com/blog/archives/115 如果机房马上要关门了,或者你急着要和MM约会,请直接跳到第六个自然段。我们这里说的KMP不是拿来放电影的(虽然我很喜欢这个软件),而是一种算法。KMP算法是拿来处理字符串匹配的。换句话说,给你两个字符串,你需要回答,B串是否是A串的子串(A串是否包含B串)。比如,字符串A="I'm matrix67",字符串B="matrix",我们就说B是A的子串。你可以委婉地问你的MM:“假如你要向你喜欢的人表白的话,我的名字是你的告白语中的子 阅读全文
posted @ 2011-07-30 11:46 AC_Von 阅读(654) 评论(0) 推荐(1)
摘要:邻接表实现图的深度遍历,纠结的问题啊。。。。。View Code #include <stdio.h>#include <malloc.h>#define MAX 100typedef struct arcnode/*表节点*/{ int adjvex; /*邻接点*/ struct arcnode *nextarc;/*指向下一条弧的指针*/}arcnode;typedef struct vnode /*头结点*/{ int data; /*定点信息*/ arcnode * firstarc; /*指向第一个依附该顶点的弧的指针*/}vnode, adjlist[MA 阅读全文
posted @ 2011-07-29 19:59 AC_Von 阅读(714) 评论(0) 推荐(0)
摘要:转载:http://blog.csdn.net/akof1314/article/details/4388304 图的存储方式可以用邻接矩阵来表示,我们假定顶点序号从0开始,即图G的顶点集的一般形式是V(G)={v0,vi,…,Vn-1}。以下代码测试过,为图的邻接矩阵表示方式。测试结构如下(含测试数据):View Code /************************************************************************//* 图的邻接矩阵存储结构 *//********************************************. 阅读全文
posted @ 2011-07-28 08:10 AC_Von 阅读(1004) 评论(0) 推荐(0)