巴什博奕(Bash Game)
摘要:巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规 定每次至少取一个,最多取m个。最后取光者得胜。 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走多少个,后取者都能够一次拿走剩余的物品,后者取胜。因此我们发现了如何取胜的法则:如果n=(m+1)*r+s,(r为任意自然数,s≤m),那么先取者要拿走s个物品,如果后取者拿走k(≤m)个,那么先取者再拿走m+1-k个,结果剩下(m+1)(r-1)个,以后保持这样的取法,那么先取者肯定获胜。总之,要保持给对手留下(m+1)的倍数,就能最后获胜。 这个游戏还可以有一种变相的玩法:两个人轮流报数...
阅读全文
posted @
2012-05-31 22:46
Try86
阅读(735)
推荐(0)
pku 2187(最远点对,旋转卡壳)
摘要:1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 5 using namespace std; 6 7 const int N = 50005; 8 9 //旋转卡壳(变化)求凸包直径O(n)10 struct point {11 int x, y;12 }p[N];13 14 int dis(point A, point B) {15 return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y);16 }17 18 i
阅读全文
posted @
2012-05-31 07:41
Try86
阅读(220)
推荐(0)
pku 1149最大流(标号法)
摘要:1 /* 2 * 一般增广路算法--Ford_Fulkerson算法(标号法) 3 */ 4 /* 5 建图:将顾客看作除源点和汇点以外的节点,源点和每个猪圈的第一个顾客连边,边的权是开始时猪 6 圈中猪的数目,若源点和某个节点之间有重边,则将权合并,顾客j紧跟在顾客i之后打开某个猪圈, 7 则边<i,j>的权是+∞,每个顾客和汇之间连边,边的权是顾客所希望购买的猪的数目。 8 */ 9 10 #include <cstdio>11 #include <cstring>12 #include <iostream>13 14 using names
阅读全文
posted @
2012-05-30 22:25
Try86
阅读(784)
推荐(0)
pku 2299Ultra-QuickSort(归并排序求逆序数)
摘要:1 /* 2 * 归并排序求逆序对 3 */ 4 5 #include <cstdio> 6 #include <iostream> 7 8 using namespace std; 9 10 const int N = 500005;11 12 long long ans;13 int a[N], b[N];14 15 void mergeSort(int left, int right) {16 int i, j, k, mid;17 if (left < right) {18 mid = (left + right) >> 1;19 ...
阅读全文
posted @
2012-05-16 20:51
Try86
阅读(155)
推荐(0)
RQNOJ 15采药(0/1背包)
摘要:1 /* 2 * 0/1背包 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 8 using namespace std; 9 10 const int N = 105;11 const int M = 1005;12 13 int f[M];14 struct pack {15 int cost;16 int value;17 }p[N];18 19 void ZeroOnePack(int n, int cost, int value) {20 for (int
阅读全文
posted @
2012-05-16 20:43
Try86
阅读(147)
推荐(0)
RQNOJ 2开心的金明(0/1背包)
摘要:1 /* 2 * 0/1背包 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 8 using namespace std; 9 10 const int M = 30;11 const int N = 30005;12 13 int f[N];14 struct pack {15 int cost;16 int value;17 }p[M];18 19 void ZeroOnePack(int n, int cost, int value) {20 for (int
阅读全文
posted @
2012-05-16 20:33
Try86
阅读(167)
推荐(0)
RQNOJ 173(归并排序求逆序数)
摘要:1 /* 2 * 归并排序求逆序对 3 */ 4 5 #include <cstdio> 6 #include <iostream> 7 8 using namespace std; 9 10 const int N = 20005;11 12 long long ans;13 int a[N], b[N];14 15 void mergeSort(int left, int right) {16 int i, j, k, mid;17 if (left < right) {18 mid = (left + right) >> 1;19 ...
阅读全文
posted @
2012-05-14 22:51
Try86
阅读(159)
推荐(0)
RQNOJ 195校园迷宫(简单BFS)
摘要:1 /* 2 * 简单BFS 3 */ 4 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 9 using namespace std;10 11 const int N = 2005;12 13 int map[N][N];14 bool vis[N][N];15 struct node {16 int x;17 int y;18 int c;19 }Q[N*N], s, e;20 int front, rear;21 int dir[4][2] = {{0, 1}, {..
阅读全文
posted @
2012-05-14 12:40
Try86
阅读(330)
推荐(0)
RQNOJ 153数的计算(简单dfs)
摘要:#include <cstdio>#include <iostream>using namespace std;int ans;void dfs(int n) { for (int i=1; i<=n/2; ++i) { ++ans; dfs(i); }}int main() { int n; while (scanf("%d", &n) != EOF) { ans = 1; dfs(n); printf ("%d\n", ans); } return 0;}
阅读全文
posted @
2012-05-13 13:54
Try86
阅读(163)
推荐(0)
RQNOJ 34紧急救援(简单BFS)
摘要:#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int N = 1005;char map[N][N], vis[N][N];struct node { int x; int y; int c;}Q[N*N], s, e;int front, rear;int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int BFS(int n) { node first, next; front = rear
阅读全文
posted @
2012-05-13 13:33
Try86
阅读(168)
推荐(0)
RQNOJ 358线段
摘要:#include <cmath>#include <cstdio>#include <iostream>using namespace std;double dis(double x1, double y1, double x2, double y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}int main() { double x1, y1, r1, x2, y2, r2; while (scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1,
阅读全文
posted @
2012-05-13 08:23
Try86
阅读(161)
推荐(0)
RQNOJ 411area(求简单多边形面积)
摘要:/** 求简单多边形面积 */ #include <cstdio>#include <iostream>using namespace std;const int N = 105;struct point { int x; int y;}p[N];int crossProd(point A, point B) { return A.x*B.y - A.y*B.x;}int compArea(int n) { p[n] = p[0]; int area = 0; for (int i=0; i<n; ++i) area += crossProd(p[i], p[i+
阅读全文
posted @
2012-05-13 07:36
Try86
阅读(169)
推荐(0)
RQNOJ 150轰炸(求最多有几个点共线)
摘要:/** 题意:求最多有多少个点共线 * 思路:枚举两点求直线,枚举有多少个点在直线上 */#include <cstdio>#include <iostream>using namespace std;const int N = 705;struct point { int x; int y;}p[N];int solve(int n) { int a, b, c, counts, ans = -1; for (int i=0; i<n; ++i) { for (int j=i+1; j<n; ++j) { a = p[j].y - p[i].y; //一般
阅读全文
posted @
2012-05-13 07:26
Try86
阅读(279)
推荐(0)
hdu 2896(AC自动机)
摘要:1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <iostream> 5 6 using namespace std; 7 8 const int N = 205; 9 const int L = 10005; 10 const int M = 100010; 11 12 int ans[5]; 13 char pat[N], str[L]; 14 struct node { 15 int n; 16 node *prefix; 17 node *.
阅读全文
posted @
2012-05-12 20:20
Try86
阅读(325)
推荐(0)
hdu 2222(AC自动机)
摘要:参考:http://www.cppblog.com/mythit/archive/2009/04/21/80633.html 1 /* 2 * AC自动机 3 */ 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <iostream> 8 9 using namespace std;10 11 const int N = 51;12 const int L = 500001;13 const int M = 1000001;14 15 char pa
阅读全文
posted @
2012-05-09 13:03
Try86
阅读(191)
推荐(0)
hdu 3172(并查集+字典树映射)
摘要:1 /* 2 * 并查集+字典树 3 */ 4 5 #include <cstdio> 6 #include <cstdlib> 7 #include <cstring> 8 #include <iostream> 9 10 using namespace std;11 12 const int N = 21;13 const int M = 200005;14 15 int cs;16 int parent[M];17 char strA[N], strB[N];18 struct node {19 int c;20 node *child[5
阅读全文
posted @
2012-05-08 18:27
Try86
阅读(377)
推荐(0)
hdu 1671(字典树)
摘要:1 /* 2 * 字典树 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <iostream> 8 9 using namespace std;10 11 const int N = 11;12 13 bool yes;14 char str[N];15 struct node {16 bool flag;17 node *child[10];18 node() {19 flag = false;20 for (int i=...
阅读全文
posted @
2012-05-08 07:11
Try86
阅读(142)
推荐(0)
hdu 1247(字典树)
摘要:1 /* 2 * 字典树 3 * 思路:把所有单词建成一棵字典树,然后把每个分成两部分,判断这两部分是否在树中 4 * 是,就符合条件。 5 */ 6 7 #include <cstdio> 8 #include <cstdlib> 9 #include <cstring>10 #include <iostream>11 12 using namespace std;13 14 const int N = 16;15 const int M = 50005;16 17 char strL[N], strR[N], dic[M][N]; 18 st
阅读全文
posted @
2012-05-08 07:03
Try86
阅读(742)
推荐(0)
hdu 1251(字典树)
摘要:1 /* 2 * 字典树 3 */ 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cstring> 7 #include <iostream> 8 9 using namespace std;10 11 const int N = 11;12 13 char str[N];14 struct node {//节点数据 15 int c; //统计前缀个数 16 node *child[26];17 node() {//初始化 18 c = 1;19 for (int i...
阅读全文
posted @
2012-05-07 21:52
Try86
阅读(125)
推荐(0)
hdu 1686(kmp)
摘要:1 /* 2 * kmp 3 */ 4 #include <cstdio> 5 #include <iostream> 6 7 using namespace std; 8 9 const int N = 10005;10 const int M = 1000005;11 12 int next[N];13 char pat[N], str[M];14 15 void indexNext() {16 int k = 0;17 next[1] = 0;18 for (int i=2; pat[i]; ++i) {19 while (k && pat...
阅读全文
posted @
2012-05-07 17:41
Try86
阅读(185)
推荐(0)
hdu 3336(kmp+dp)
摘要:摘自:http://www.cnblogs.com/wuyiqi/archive/2012/01/05/2313746.html题意:求给定字符串含前缀的数量abab前缀为aababaabababab中共有六个子串是前缀a a ab ab aba abab所以答案为6利用kmp中的匹配原理可以完美的解决此题a---------d----- -----a---------d i j如上所示,假设两串字符完全相等,next[j]=i,代表s[1...i]==sum[j-i+1....j],这一段其实就是前缀i~j之间已经不可能有以j结尾的子串是前缀了,不然next【j】就不是 i 了设dp【i】:
阅读全文
posted @
2012-05-07 12:56
Try86
阅读(1015)
推荐(0)
hdu 3746(kmp)
摘要:1 /* 2 * kmp 3 */ 4 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 9 using namespace std;10 11 const int N = 100005;12 13 int next[N];14 char pat[N];15 16 void indexNext() {17 int k = 0;18 next[1] = 0;19 for (int i=2; pat[i]; ++i) {20 while (k && pat[k+1]!
阅读全文
posted @
2012-05-07 07:12
Try86
阅读(195)
推荐(0)
hdu 2594(kmp)
摘要:1 /* 2 * KMP 3 * 思路:把两个串连接,然后求next[]数组值 4 * 注意:当两个串都是重复串且重复子串一样是,要特殊处理 5 */ 6 7 #include <cstdio> 8 #include <iostream> 9 10 using namespace std;11 12 const int N = 50005;13 14 int next[N<<1];15 char str[N<<1], pat[N];16 17 void indexNext() {18 int k = 0;19 next[1] = 0;20 ...
阅读全文
posted @
2012-05-07 07:01
Try86
阅读(283)
推荐(0)
hdu 2087(kmp)
摘要:1 /* 2 * kmp 3 */ 4 5 #include <cstdio> 6 #include <iostream> 7 8 using namespace std; 9 10 const int N = 1005;11 12 int next[N];13 char str[N], pat[N];14 15 void indexNext() {16 int k = 0;17 next[1] = 0;18 for (int i=2; pat[i]; ++i) {19 while (k && pat[k+1]!=pat[i]) k = next...
阅读全文
posted @
2012-05-06 21:52
Try86
阅读(163)
推荐(0)
hdu 1358(kmp)
摘要:1 /* 2 * kmp 3 * 利用next[]数组的性质求周期 4 */ 5 #include <stdio.h> 6 #include <string.h> 7 #define N 1000005 8 9 int next[N];10 char str[N];11 12 void indexNext() {13 int i, k = 0;14 next[1] = 0;15 for (i=2; str[i]; ++i) {16 while (k && str[k+1]!=str[i]) k = next[k];17 if (...
阅读全文
posted @
2012-05-06 21:40
Try86
阅读(163)
推荐(0)
hdu 1711(kmp)
摘要:1 /* 2 * kmp 3 */ 4 #include <cstdio> 5 #include <iostream> 6 7 using namespace std; 8 9 const int N = 10005;10 const int M = 1000005;11 12 int str[M], pat[N], next[N];13 14 void indexNext(int lenPat) {15 int k = 0;16 next[1] = 0;17 for (int i=2; i<=lenPat; ++i) {18 while (k &...
阅读全文
posted @
2012-05-06 21:16
Try86
阅读(154)
推荐(0)
hdu 2203(kmp)
摘要:1 /* 2 * kmp 3 */ 4 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 9 using namespace std;10 11 const int N = 100005;12 13 char str[N<<1];14 char pat[N];15 int next[N];16 17 void indexNext() {18 int k = 0;19 next[1] = 0;20 for (int i=2; pat[i]; ++i) {21 while
阅读全文
posted @
2012-05-06 20:28
Try86
阅读(197)
推荐(0)
hdu 2141(排序+二分)
摘要:1 /* 2 * 排序+二分 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #define N 501 8 9 int a[N], b[N*N], c[N];10 11 int cmp(const void *a, const void *b) {12 return *(int *)a - *(int *)b;13 }14 15 int main() {16 int l, n, m, s, i, j, k, tmp, tmpp, left, right, mid, flag, cas = 0;17 wh...
阅读全文
posted @
2012-05-05 22:52
Try86
阅读(215)
推荐(0)