Fork me on GitHub

02 2014 档案

摘要:一直向前搜。。做法有点像模拟。但是要用到出队入队,有点像搜索。代码:#include #include #include #include #include #include using namespace std;#define N 100003struct node{ int p,d; bool operator a.d; return p>a.p; }}stone[N];int maxdis;priority_queue que;void GO(){ node now,next; int OE = 1; while(!que.empty(... 阅读全文
posted @ 2014-02-28 22:25 whatbeg 阅读(242) 评论(0) 推荐(0)
摘要:优先队列。。刚开始用蠢办法,经过一个vector容器中转,这么一来一回这么多趟,肯定超时啊。超时代码如下:#include #include #include #include #include #include #include #include #include #include #include using namespace std;#define N 30003priority_queue,greater > que;int A[N];vector tmp;int main(){ int m,n,i,j,th,u,k,h; while(scanf("%d%d" 阅读全文
posted @ 2014-02-28 21:56 whatbeg 阅读(230) 评论(0) 推荐(0)
摘要:好题。这题可以有三种解法:1.Dijkstra 2.优先队列 3.并查集我这里是优先队列的实现,以后有时间再用另两种方法做做。。方法就是每次都选当前节点所连的权值最大的边,然后BFS搜索。代码:#include #include #include #include #include #include #include #include #include #include #include using namespace std;#define N 100007struct node{ int ind,wt; bool operator mp;priority_queue qu... 阅读全文
posted @ 2014-02-28 10:33 whatbeg 阅读(317) 评论(0) 推荐(0)
摘要:维护一个单调栈,保持从大到小的顺序,每次加入一个元素都将其推到尽可能栈底,知道碰到一个比他大的,然后res+=tail,说明这个cow的头可以被前面tail个cow看到。如果中间出现一个超级高的,自然会推到栈底,即此元素以前的cow看不到此元素后面cow的头,即res不会加此元素前面的个数,说明是正确的。注意要用long long 或者 __int64类型。 刚开始看着80000不大的样子,其实最大情况下res有 1+2+....+80000 = 3200040000,超过int范围。以后还是验证一下,或者看到这种比较大的数还是优先用long long 或者 __int64类型吧。代码:#in 阅读全文
posted @ 2014-02-26 14:37 whatbeg 阅读(194) 评论(0) 推荐(0)
摘要:做法:维护一个单调递减序列,只需输出序列中的第一个元素即可。对于命令我们可以进行不同的处理:如果是Q命令,则判断当前队列中是否仍有元素,如果没有则输出-1,如果有则直接输出队首。如果是G命令,则对last加1,之后对于队列中所有超出范围的前端元素进行出队操作。(该元素在原序列中的位置>=last)如果是C命令,则将该元素加入队列中,并和队尾元素比较,维护队列的单调性。这里考虑一个问题,当前元素加如后对队尾元素为什么可以毫无保留的删去呢?因为当前元素面试时间一定比队尾元素晚,所以如果当前元素比队尾元素大,则在当前元素被删去之前,删去队尾元素不影响最大值。代码:#include #inclu 阅读全文
posted @ 2014-02-26 09:20 whatbeg 阅读(332) 评论(0) 推荐(0)
摘要:重新刷这个经典题,感觉跟以前不一样了,变得更加容易理解了,不讲解了,看代码。注意:要用C++提交,用G++会超时。。代码:#include #include #include using namespace std;#define N 1000007int a[N],mp[N],head,tail,n,k;inline void pushup(int i){ while(tail > head && a[i] head && a[i] > a[mp[tail-1]]) tail--; mp[tail++] = i;}void solve(int cm 阅读全文
posted @ 2014-02-26 09:14 whatbeg 阅读(261) 评论(0) 推荐(0)
摘要:解法:因为至多20行,所以至多建20棵线段树,每行建一个。具体实现如下,有些复杂,慢慢看吧。#include #include #include #include #include using namespace std;#define N 1000010struct node{ int mi... 阅读全文
posted @ 2014-02-22 23:01 whatbeg 阅读(379) 评论(0) 推荐(0)
摘要:1.POJ 3450 Coporate Identity这两题的解法都是枚举子串,然后匹配,像这种题目以后可以不用KMP来做,直接字符串自带的strstr函数搞定,如果字符串未出现,该函数返回NULL。下面贴出其比较。代码:(KMP版)(1360ms 888KB)#include #include #include #include #include using namespace std;#define N 4007char ans[203],str[203];char ss[N][203],tt[203];int next[203];void getnext(char *ss){ i... 阅读全文
posted @ 2014-02-22 14:26 whatbeg 阅读(321) 评论(0) 推荐(0)
摘要:A. Codeforces 92A Chips签到题。。#include #include #include #include #include using namespace std;#define N 10007int a[55];int main(){ int n,m,i; whi... 阅读全文
posted @ 2014-02-20 13:51 whatbeg 阅读(354) 评论(0) 推荐(0)
摘要:1.HDU 1711 Number Sequence代码:#include #include #include #include #include using namespace std;#define N 10007int a[1000007],b[N],next[N];int n,m;void getnext(){ next[0] = -1; int i = 0,j = -1; while(i#include #include #include #include using namespace std;#define N 10007int a[1000007],b[N],... 阅读全文
posted @ 2014-02-19 15:00 whatbeg 阅读(194) 评论(0) 推荐(0)
摘要:字典树较简单题,无需维护标记,注意细节即可。代码:#include #include #include using namespace std;#define N 100027struct node{ node *next[2];}*root;char ss[10][13];node *create(){ node *p; p = (node *)malloc(sizeof(node)); for(int i=0;inext[i] = NULL; return p;}void release(node *p){ for(int i=0;inext[i] !=... 阅读全文
posted @ 2014-02-12 16:33 whatbeg 阅读(220) 评论(0) 推荐(0)
摘要:较简单字典树,每输入一对字符串,前一个放在字典(数组)中,后一个插入字典树中,并将其最终的flag赋为前一个在数组中的下标,再就好找了。输入的处理方法要注意一下。 代码:#include #include #include #include #include #include #include #include using namespace std;#define N 100027struct node{ int flag; node *next[26];}*root;char dic[N][11];node *create(){ node *p; p = (nod... 阅读全文
posted @ 2014-02-11 16:47 whatbeg 阅读(208) 评论(0) 推荐(0)
摘要:线段树较复杂题,涵盖了线段树的大部分操作。 这题节点维护: ls:左边最长连续1的长度, rs:右边最长连续1的长度 , ms:整个区间的最长连续1的长度, sum:区间内1的个数 ,mark:操作懒标记 将取反操作单独做一个函数来处理。 具体维护见代码:#include #include #include #include #include using namespace std;#define N 100027struct node{ int ls,rs,ms; int sum; int mark;}tree[4*N];int a[N];int n,m;void pus... 阅读全文
posted @ 2014-02-07 13:27 whatbeg 阅读(205) 评论(0) 推荐(0)
摘要:字典树又一基本题代码:#include #include #include #include #include using namespace std;#define N 1027struct node{ int count; node *next[30];}*root;char ss[13];node *create(){ node *p; p = (node *)malloc(sizeof(node)); p->count = 1; for(int i=0;inext[i] = NULL; return p;}void release(node *... 阅读全文
posted @ 2014-02-03 14:29 whatbeg 阅读(218) 评论(0) 推荐(0)
摘要:字典树基本题。代码:#include #include #include #include #include #include using namespace std;#define N 1027struct node{ int count; node *next[30];}*root;char ss[N][30];node *create(){ node *p; p = (node *)malloc(sizeof(node)); p->count = 1; for(int i=0;inext[i] = NULL; return p;}void rel... 阅读全文
posted @ 2014-02-03 14:25 whatbeg 阅读(248) 评论(0) 推荐(0)
摘要:递归求解,代码不太好看,是2013年7月写的代码:#include#include#include#include#includeusing namespace std;char s[102][102];int flag[26];int res[28];int tag1[27];int zong,heng;int tag;int n,m;int zhao(int qix,int qiy,int zhx,int zhy){ int fff=0; for(int i=qix;i=2&&heng-j>=2) { for(... 阅读全文
posted @ 2014-02-02 11:23 whatbeg 阅读(257) 评论(0) 推荐(0)