04 2013 档案
HDU2174 kiki's game(博弈)
摘要:题目链接。分析:博弈论水题。要使行列都走到关键数.如果行是关键数,列不是,就向下走一;如果行是行不是关键数,列是,就想左走一;如果行列都不是,就想左下走一;如果行列都是的话,就pity了。#include <iostream>#include <algorithm>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>#include <stack>#include <map>using namespace std; 阅读全文
posted @ 2013-04-29 23:51 Still_Raining 阅读(218) 评论(0) 推荐(0)
uva11991 Easy Problem from Rujia Liu?
摘要:题目链接。分析:《算法竞赛入门经典——训练指南》上的一道例(水)题,map的应用,个人感觉代码中注释掉的那一句没有什么意义,就注释掉了,提交确实也对了。map的小知识点(总结自c++ primer):对于map容器,如果下标所表示的键在容器中不存在,则添加元素。书中的例子:map<string, int> word_count;sting word;while(cin>>word) ++word_count[word];在单词第一次出现时,会在word_count中创建并插入一个以该单词为索引的新元素,同时将它的值初始化为0。当只想要知道某元素存在,而当元素不存在时,并 阅读全文
posted @ 2013-04-29 17:56 Still_Raining 阅读(188) 评论(0) 推荐(0)
UVA1394 And Then There Was One
摘要:题目链接。分析:该题目是一个约瑟夫环的变形,区别就是第一个删除的数是m。该题的n和k都比较大,链表法O(nk),是不行的。因为只关心最后一个被删除的编号,而不需要完整的删除顺序,可以用递推法求解。先分析一下传统的约瑟夫环:n个人,编号0~n-1,每喊道k该人就被淘汰,直到最后剩下一个人。公式为:f(n) = (f(n-1) + k) % n,f(1)=0(这里的f(n)为最后一个人在序列剩余n个人时的编号)公式推导:初始的序号为(一)0, 1, ..., q-1, q, q+1, ..., n-1设q = k % n, 出列q-1后,序列变为(二)0, 1, ..., q-2, q, q+1, 阅读全文
posted @ 2013-04-28 13:19 Still_Raining 阅读(271) 评论(0) 推荐(0)
uva1330 City Game
摘要:题目链接。分析:《训练指南》上的代码是二维的,优化了下,改成了一维的。#include <iostream>#include <vector>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>using namespace std;const int maxn = 1000 + 10;int _left[maxn], _right[maxn], _up[maxn];char mat[maxn];int main(){ int T, 阅读全文
posted @ 2013-04-23 12:51 Still_Raining 阅读(169) 评论(0) 推荐(0)
UVA1276 Network
摘要:题目链接。分析:《训练指南》上的代码,写的不是一般的漂亮。贴之以珍藏。#include <iostream>#include <vector>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>using namespace std;const int maxn = 1000 + 10;vector<int> g[maxn], nodes[maxn];int fa[maxn], k, n;bool covered[maxn 阅读全文
posted @ 2013-04-20 19:45 Still_Raining 阅读(189) 评论(0) 推荐(0)
SDUT2408 Pick apples(贪心+完全背包)
摘要:题目链接。分析:贪心不能解决背包问题在于,它无法保证能将背包装满,部分闲置的背包空间使每公斤背包空间的价值降低了。因此可以大范围用贪心,小范围用背包。注意:提交过程中WA了很多次,原因已查明, WA时用的int p, v;而AC用的是long long p, v; 题目中已明确给出数据范围,按理说int是可以的,不明白为什么会WAAC代码如下:#include <iostream>#include <vector>#include <cstring>#include <algorithm>#include <cstdio>#inclu 阅读全文
posted @ 2013-04-20 16:23 Still_Raining 阅读(338) 评论(0) 推荐(0)
UVA11729 Commando War
摘要:题目链接。分析:自己写的时候是直接模拟的,但似乎训练指南上的解法更好。偶的代码:View Code #include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int maxn = 1010;struct Job{ int b, j;}a[maxn];int cmp(const Job &x, const Job &y){ return x.j > y.j;}int main(){ int n, cnt=0; while(cin> 阅读全文
posted @ 2013-04-16 13:04 Still_Raining 阅读(261) 评论(0) 推荐(0)
Emergency(最短路,floyd)
摘要:Emergency【Description】Kudo’s real name is not Kudo. Her name is Kudryavka Anatolyevna Strugatskia, andKudo is only her nickname.Now, she is facing an emergency in her hometown:Her mother is developing a new kind of spacecraft. This plan costs enormous energy butfinally failed. What’s more, because o 阅读全文
posted @ 2013-04-13 23:15 Still_Raining 阅读(335) 评论(0) 推荐(0)
POJ3254 Corn Fields(状态压缩DP)
摘要:题目链接。分析:作为状态压缩DP入门题,很好。由题意可知:1.任意两牛(包括行和列)不能临近2.牛只能在有草的即值为1的地方3.不放牛也为一种方案定义dp[i][s]为第i行状态s下的方案数。dp[i][s] =∑(dp[i-1][s']),其中s'为不与s冲突的状态注意:因为位运算优先级很低,所以要多加括号。因此if((cow & (cow<<1)) != 0) return 0;和if(cow & (cow<<1) != 0) return 0;是不一样的#include <cstdio>#include <cstr 阅读全文
posted @ 2013-04-11 13:37 Still_Raining 阅读(255) 评论(0) 推荐(0)
状态压缩动态规划(直播)
摘要:很强悍的算法,至今,仍旧迷迷糊糊。先将看懂的代码贴上来。牛人讲解:http://blog.csdn.net/lhshaoren/article/details/7526480以下代码是转自牛人的。#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>using namespace std;#define MAXN 21const double INF = 1<<23;struct Node{ int x 阅读全文
posted @ 2013-04-10 21:29 Still_Raining 阅读(232) 评论(0) 推荐(0)
POJ2503 Babelfish(二分)
摘要:题目链接。分析;主要是学着用一下bsearch。#include <stdio.h>#include <stdlib.h>#include <string.h>struct Entry{ char english[15], foreign[15];}entrys[100010];int cmp(const void *a, const void *b){ return strcmp((*(struct Entry *)a).foreign, (*(struct Entry *)b).foreign);}int b_cmp(const void *a, con 阅读全文
posted @ 2013-04-07 12:09 Still_Raining 阅读(189) 评论(0) 推荐(0)
POJ1276 Cash Machine(多重背包)
摘要:题目链接。分析:该题为多重背包。有了上一次的经验(HDU2191),这道题也就了了了。本题的思路:将cash想象成包的容量,将面值Dk想象成两个条件,即体积和价值都为Dk,那么本题就自然而然的成了求最大价值的问题。另外,这次尝试了一下其它的代码风格,因为曾经听说过,试了一下,写起来感觉不错,但找起来错就不那么容易了。纠结#define For(i, i0, n) for(int i=i0; i<=n; i++),是将它定义成i<=n呢还是i<n呢?在敲代码的过程中错认为是i<n,找了很久BUG。#include <cstdio>#include <cs 阅读全文
posted @ 2013-04-06 12:32 Still_Raining 阅读(207) 评论(0) 推荐(0)
程序员技术练级攻略(转)
摘要:转自(http://coolshell.cn/articles/4990.html)月光博客6月12日发表了《写给新手程序员的一封信》,翻译自《An open letter to those who want to start programming》,我的朋友(他在本站的id是Mailper)告诉我,他希望在酷壳上看到一篇更具操作性的文章。因为他也是喜欢编程和技术的家伙,于是,我让他把他的一些学习Python和Web编程的一些点滴总结一下。于是他给我发来了一些他的心得和经历,我在把他的心得做了不多的增改,并根据我的经历增加了“进阶”一节。这是一篇由新手和我这个老家伙根据我们的经历完成的文章。 阅读全文
posted @ 2013-04-06 11:43 Still_Raining 阅读(271) 评论(0) 推荐(0)
回溯法(挑战编程)
摘要:回溯法,在以前也是会的,只不过没有系统的学习,只是从题目中接触了一些。今天,系统的学习了一下,颇有感触。对于回溯,书上的模版是这样的:bool finished = FLASE; /*found all solutions yet? */backtrack(int a[], int k, data input){ int c[MAXCANDIDATES]; // candidates for next position int ncandidates; //next position candidate count int i; //counter if(i... 阅读全文
posted @ 2013-04-05 16:54 Still_Raining 阅读(257) 评论(0) 推荐(0)
找出2n+1个数中不成对的那个
摘要:在网上看到一篇相关的文章,感叹算法的巧妙。用O(n)复杂度搞定。异或操作(^)——(对于位操作)相同为0,相异为1.比如:1^0 = 1, 1 ^1=0这样:两个相同的数异或就为0任何数和0异或为自己(转化到位。1^0 =1,0^0=0对于2,1,3,2,1, (2^2)^(1^1)^3=3.如此就能将不成对的3找出来。异或具有交换律,所以可以按顺序计算,2^1^3^2^1=3。代码如下:#include <stdio.h>#include <string.h>#include <stdlib.h>int a[7] = {1, 2, 1, 2, 3, 5, 阅读全文
posted @ 2013-04-05 13:25 Still_Raining 阅读(246) 评论(0) 推荐(0)
HDU1210 Eddy's 洗牌问题
摘要:题目链接。分析:发现只要模拟1的变化就很水了。#include <stdio.h>#include <string.h>#include <stdlib.h>int main(){ int n, t, cnt; while(scanf("%d", &n) == 1){ t = 2; cnt = 1; while(t != 1){ if(t <= n) t *= 2; else t=(t-n-1)*2+1; cnt++; } printf("%d\n... 阅读全文
posted @ 2013-04-04 22:39 Still_Raining 阅读(178) 评论(0) 推荐(0)
UVA10006 Carmichael Numbers(数论)
摘要:题目链接。分析:很简单,只是用到了一个模运算的公式。xy mod n = (x mod n)y mod n递归调用函数就OK了。注意:int 是存不了的。看n很小,没注意,用int WA了很多次。因为65000^2=4225000000,很大哦。所以可以用unsigned 或者是 long之类的#include <stdio.h>#include <stdlib.h>long is_prime(long n){ int i; for(i=2; i*i<=n; i++){ if(n % i == 0) break; } if(i*i<=n) return 0. 阅读全文
posted @ 2013-04-04 14:49 Still_Raining 阅读(274) 评论(0) 推荐(0)
HDU2191 悼念512汶川大地震遇难同胞(多重背包)
摘要:题目链接。分析:一直在看多重背包,一开始做了几个,几乎全部TLE。原来是要用二进制优化,学习了一下(详情见本博客)。第一种做法:View Code #include <stdio.h>#include <string.h>#define MAXN 3000int n, m;int value[MAXN], weight[MAXN];int dp[150];int max(int x, int y){ return x > y ? x : y;}int main(){ int T, p, h, c, cnt, i, j; scanf("%d", 阅读全文
posted @ 2013-04-03 22:46 Still_Raining 阅读(286) 评论(0) 推荐(0)
SDUT2062 K-based Numbers(递推)
摘要:分析:这题很水,只是做的时候对边界没有分析正确,WA了很多次。发上来,警示自己要细心。方法1:设f(n)为第n为可以为0的排法:View Code #include <stdio.h> #include <stdlib.h> #include <string.h> int dp[100], k; int f(int n){ if(n == 1) return k; if(n == 2) return (k-1)*(k+1); else if(dp[n]) return dp[n]; else return (dp[n] = (k-1)*(... 阅读全文
posted @ 2013-04-02 21:53 Still_Raining 阅读(279) 评论(0) 推荐(0)
Euclid算法
摘要:看了几天挑战编程的数论,颇有感触,尤其是欧几里得算法,特此记下笔记(毕竟书是借的)。 整除:对于整数a和b, 若存在整数k使得a = bk, 则称b整除(divides)a(用b|a来表示)。b|a也可以说成b是a的约数,或者a是b的倍数(multiple)。唯一分解定理:x能唯一的表示成它的素因数的乘积。如果两个整数的最大公约数(greatest common divisor)(也称gcd)是1,称二者是互素(relatively prime)的。Euclid算法:gcd(a,b)=gcd(b,a%b)Euclid算法的证明:1.如果b|a,则gcd(a,b)= b。因为如果b整除a,则存在 阅读全文
posted @ 2013-04-02 20:17 Still_Raining 阅读(989) 评论(0) 推荐(0)
HDU1286 找新朋友
摘要:题目链接。分析:一开始竟然天真的按着题意来。果断TLE。然后就改了下。#include <stdio.h>int a[32800];int main(){ int T, n, i, j, cnt; scanf("%d", &T); while(T--){ scanf("%d", &n); for(i=1; i<n; i++) a[i] = 1; for(i=2; i<n; i++){ if(n % i == 0){ for(j=2; j<n; j++){ ... 阅读全文
posted @ 2013-04-02 18:49 Still_Raining 阅读(145) 评论(0) 推荐(0)
UVA10110 Light, more light
摘要:链接地址。分析:如果n能被a整除,那么一定存在一个b使得a*b = n。开关经两次变化相当于没有变化。那么只要看a = b的那种特殊情况就OK了。#include <stdio.h>#include <math.h>#include <stdlib.h>int main(){ unsigned n, k; while(scanf("%d", &n) == 1 && n){ k = (int)sqrt(n*1.0); if(k*k == n){ printf("yes\n"); } else pr 阅读全文
posted @ 2013-04-02 13:38 Still_Raining 阅读(146) 评论(0) 推荐(0)
POJ1837 Balance(dp)
摘要:题目链接。分析:不得不说,这是一道好题。dp[i][j],i代表前i个物品,j代表平衡度为j时的方法数。#include <stdio.h>#include <stdlib.h>#include <string.h>int dp[21][15002];int main(){ int n, g, i, j, k, t, c[21], w[21]; scanf("%d %d", &n, &g); for(i=1; i<=n; i++) scanf("%d", &c[i]); for(i=1; 阅读全文
posted @ 2013-04-01 11:29 Still_Raining 阅读(169) 评论(0) 推荐(0)