2012年3月28日

poj 2785 4 Values whose Sum is 0

摘要: 二分枚举;也可以将两个和数组都排序,这样可以在查找时保持沿一个方向,最坏情况下复杂度为O(n),不如二分查找;计算cpd[]时,c[i] + d[j]错写成c[i]+d[i]查了半天才发现。。。 1 # include <stdio.h> 2 # include <stdlib.h> 3 4 # define MAXN 4001 5 6 int apb[MAXN*MAXN], cpd[MAXN*MAXN]; 7 int a[MAXN], b[MAXN], c[MAXN], d[MAXN]; 8 9 int cmp(const void *a, const void *b 阅读全文

posted @ 2012-03-28 02:13 getgoing 阅读(435) 评论(0) 推荐(0) 编辑

2012年3月27日

UVa 10815 - Andy's First Dictionary

摘要: 这道题貌似有陷阱:用gets过不了。 1 # include <stdio.h> 2 # include <ctype.h> 3 # include <string.h> 4 # include <stdlib.h> 5 6 # define MAX_LEN 205 7 # define MAXN 5005 8 9 char dic[MAXN][MAX_LEN];10 char word[MAX_LEN];11 12 int cmp(const void *a, const void *b);13 14 int main()15 {16 char 阅读全文

posted @ 2012-03-27 00:37 getgoing 阅读(694) 评论(0) 推荐(0) 编辑

UVa 10878 - Decode the tape

摘要: 开始以为统计示例那句话中26个字母对应的字符串就行了,后来发现‘A’和‘a’不一样,仔细一看有一个位不一样,突然想到了ascii,一看7位,这不正好吗! 1 # include <stdio.h> 2 # include <string.h> 3 4 char str[15]; 5 char t[8] = {64, 32, 16, 8, 0, 4, 2, 1}; 6 7 int main() 8 { 9 short int i, c;10 11 gets(str);12 while (gets(str) != NULL)13 {14 ... 阅读全文

posted @ 2012-03-27 00:33 getgoing 阅读(216) 评论(0) 推荐(0) 编辑

UVa 409 - Excuses, Excuses!

摘要: 写的很繁琐。 1 # include <stdio.h> 2 # include <ctype.h> 3 4 # define MAX_WORD_LEN 25 5 # define MAX_LINE_LEN 75 6 # define MAXN 25 7 8 char keyw[MAXN][MAX_WORD_LEN]; 9 char line[MAXN][MAX_LINE_LEN];10 char copy[MAX_LINE_LEN];11 int cnt[MAXN];12 13 int key_cnt(char *line, int len, char *keywor 阅读全文

posted @ 2012-03-27 00:30 getgoing 阅读(350) 评论(0) 推荐(0) 编辑

TLE:csu 1205 放石子游戏

摘要: 表示第一次用dfs。。。 1 # include <stdio.h> 2 # include <string.h> 3 4 char board[1001][1001]; 5 char vis[1001]; 6 int trace[1001]; 7 int N, M, ans; 8 9 void dfs(int x, int y);10 void special_dfs(int x, int y, int trace[]);11 12 int main()13 {14 int i, j, x, y;15 16 while (~scanf("%d%d" 阅读全文

posted @ 2012-03-27 00:26 getgoing 阅读(228) 评论(0) 推荐(0) 编辑

2012年3月26日

CSU 1109 FIbonacci-prime

摘要: 实际上就是求第(<=)600000个素数,但是。。要优化内存,要优化速度。。最重要的要估计第600000个素数有多大,直接打印当然行,但是。。总之学了素数定理:1/ln(n)的分布。。 1 # include <stdio.h> 2 3 # define MAXN 600005 4 # define LN 15 5 # define INDEX(i) ((i) >> 5) 6 # define OFFSET(i) ((i) % 32) 7 # define GET_BIT(i) ((ptable[INDEX(i)]>>OFFSET(i)) & 阅读全文

posted @ 2012-03-26 00:57 getgoing 阅读(227) 评论(0) 推荐(0) 编辑

2012年3月25日

poj 1163 The Triangle

摘要: 简单的动态规划,重点是怎样才算是好的实现。 1 # include <stdio.h> 2 3 # define MAXN 100 4 5 short int a[MAXN][MAXN]; 6 7 int main() 8 { 9 int n, i, j;10 11 scanf("%d", &n);12 13 for (i = 0; i < n; ++i)14 for (j = 0; j <= i; ++j)15 scanf("%d", &a[i][j]);16 17 for (i = n... 阅读全文

posted @ 2012-03-25 15:15 getgoing 阅读(183) 评论(0) 推荐(0) 编辑

poj 3070 Fibonacci

摘要: 先计算幂乘表,根据输入n的二进制位判断是否乘该位相应的幂;以前做过类似的,但是当时递推矩阵不一样,这次拜读了Ozy的大作,自己敲了一遍;运行时间: 0ms,神奇吧! 1 /* poj 3070 */ 2 3 # include <stdio.h> 4 5 # define MAXN 30 6 # define MOD 10000 7 8 short int power[MAXN][4] = {{1,1,1,0}}; 9 short int ans[4];10 11 void mul(short int *a, short int *b, short int *c);12 13 in 阅读全文

posted @ 2012-03-25 14:37 getgoing 阅读(326) 评论(0) 推荐(0) 编辑

2012年3月23日

poj 1061 青蛙的约会

摘要: 求线性同余方程的最小非负解;gcd(a,n)=d;若d|b则有d个解,分布在[0,n-1]上,周期为n/d,所以取[0,n/d-1]上的即可(取模)。/* 谁让你试int型,WA活该 */ 1 # include <stdio.h> 2 3 long long int d, xx, yy; 4 5 long long int extended_euclid(long long int a, long long int b) 6 { 7 long long int t; 8 if (!b) d = a, xx = 1, yy = 0; 9 else 10 {1... 阅读全文

posted @ 2012-03-23 18:11 getgoing 阅读(240) 评论(0) 推荐(0) 编辑

2012年3月22日

csu 1102 Palindrome

摘要: (当然,思路是大牛的)找出反转串与原串的最长公共子列(不连续),然后总长度减去这个LCS的长度即可;dp,空间优化是显然可以的,但是……先AC了再说。。;3WA:题目要求大小写是distinct,没有仔细读题就想当然地把大写转为小写了; 求c[i][j]时,比较的是x[i-1]与y[j-1]; 将c定义为字符型数组,显然通不过,字符型最大才127,就改为int吧。。1MLE:接上,int型的c会严重MLE的,题目要求最大长度为5000,short足够用,再改为short。/* LCS 问题 */# include <stdio.h># define MAX(a,b) ((a)> 阅读全文

posted @ 2012-03-22 23:46 getgoing 阅读(189) 评论(0) 推荐(0) 编辑

2012年3月21日

美文一篇。。。

摘要: http://roba.rushcj.com/?p=548 阅读全文

posted @ 2012-03-21 22:51 getgoing 阅读(118) 评论(0) 推荐(0) 编辑

TLE:csu 1016 最小公倍数

摘要: 求删改方案总共多少种,预料之中的超时,因为所有提交的22次中有12次超时。 1 # include <stdio.h> 2 3 # define MAXN 205 4 5 int gcd(int a, int b); 6 7 int m[MAXN]; 8 int f[MAXN][MAXN]; 9 10 int main()11 {12 int i, j, T, n, lcm, tmp, cnt;13 14 scanf("%d", &T);15 while (T--)16 {17 cnt = 0;18 ... 阅读全文

posted @ 2012-03-21 01:25 getgoing 阅读(499) 评论(2) 推荐(1) 编辑

HDOJ 1108 最小公倍数

摘要: 题目来源:POJ。。。# include <stdio.h>int gcd(int x, int y);int main(){ int x, y; while (~scanf("%d%d", &x, &y)) printf("%d\n", x/gcd(x,y)*y); return 0;}int gcd(int x, int y){ while (x != y) if (x > y) x = x - y; else y = y - x;}下面这个是不对的,原因是Runtim... 阅读全文

posted @ 2012-03-21 01:22 getgoing 阅读(273) 评论(0) 推荐(0) 编辑

2012年3月20日

csu 1010 Water Drinking

摘要: 并查集变种;# include <stdio.h># include <memory.h># define MAXN 100005int pre[MAXN];int dis[MAXN];int main(){ int n, x, y, i, mind, ans; while (~scanf("%d", &n)) { memset(dis, 0, sizeof(dis)); for (i = 0; i < MAXN; ++i) pre[i] = i; while (n--) { ... 阅读全文

posted @ 2012-03-20 23:04 getgoing 阅读(293) 评论(0) 推荐(0) 编辑

csu 1004 Xi and Bo

摘要: 并查集;/* 下面的问题已经找到原因:dev c++使用c++编译器,变量end的命名可能出现冲突,使用gcc编译不会出现报错*/遇到了一个诡异的问题:全局变量放在main外报错。# include <stdio.h># define MAXN 105int father[MAXN];int main(){ int T, sta, end, n, m, x, y; int i; scanf("%d", &T); while (T--) { scanf("%d%d%d", &sta,&end,&n); f... 阅读全文

posted @ 2012-03-20 17:45 getgoing 阅读(553) 评论(3) 推荐(0) 编辑

csu 1009 抛硬币

摘要: 稍微演算一下,就会发现只要把每一列大小次序依次选择元素,然后相乘相加,结果一定是最大的;比如a > b, c > da cb dac+bd > ad+bc (移向做差),然后扩展到多行多列的情况就行了。有一条警告,不知道怎么排除。29 *\csu1009.c [Warning] passing arg 4 of `qsort' from incompatible pointer type 1 # include <stdio.h> 2 # include <stdlib.h> 3 4 # define LMAX 105 5 # define C 阅读全文

posted @ 2012-03-20 17:43 getgoing 阅读(484) 评论(2) 推荐(1) 编辑

[转载]register修饰符

摘要: 以下内容转载自百度百科:Register register修饰符暗示编译程序相应的变量将被频繁地使用,如果可能的话,应将其保存在CPU的寄存器中,以加快其存储速度。例如下面的内存块拷贝代码, 但是使用register修饰符有几点限制。 首先,register变量必须是能被CPU所接受的类型。这通常意味着register变量必须是一个单个的值,并且长度应该小于或者等于整型的长度。不过,有些机器的寄存器也能存放浮点数。 其次,因为register变量可能不存放在内存中,所以不能用“&”来获取register变量的地址。 由于寄存器的数量有限,而且某些寄存器只能接受特定类... 阅读全文

posted @ 2012-03-20 00:30 getgoing 阅读(287) 评论(0) 推荐(0) 编辑

2012年3月18日

csu 1008 Horcrux

摘要: 换成栈就解决了超时,时间上还可以优化。 1 # include <stdio.h> 2 # include <memory.h> 3 4 # define MAXN 100005 5 6 unsigned short s[MAXN]; 7 int top; 8 9 int main()10 {11 int n, x, i, f, t, tot, top;12 13 while (~scanf("%d", &n))14 {15 f = top = tot = 0;16 memset(s, 0, sizeof(... 阅读全文

posted @ 2012-03-18 23:39 getgoing 阅读(483) 评论(0) 推荐(0) 编辑

HDOJ 2473 并查集

摘要: 就知道会超时,时限 8ms,1 ≤ N ≤ 105 , 1 ≤ M ≤ 106,循环查找当然超时;有点新意的并查集。 1 # include <stdio.h> 2 3 # define MAXN 100005 4 5 int father[MAXN]; 6 7 int main() 8 { 9 int N, M, i, cnt, ch, x, y, f, tmp, p, clo = 0;;10 11 while (1)12 {13 scanf("%d%d", &N, &M);14 getchar();1... 阅读全文

posted @ 2012-03-18 17:41 getgoing 阅读(247) 评论(0) 推荐(0) 编辑

HDOJ1213 并查集

摘要: 格式要求是:DO NOT output any blanks.千万不要理解成:DO NOT output any blanks or blank lines.我理解成:不要输出多余的行。# include <stdio.h># define MAXN 1002int father[MAXN];int main(){ int N, M, x, y, T, i, cnt; scanf("%d", &T); while (T--) { cnt = 0; scanf("%d%d", &N, &M); ... 阅读全文

posted @ 2012-03-18 17:11 getgoing 阅读(335) 评论(2) 推荐(1) 编辑

导航