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
阅读(187)
推荐(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)
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
阅读(165)
推荐(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
阅读(186)
推荐(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
阅读(335)
推荐(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
阅读(257)
推荐(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
阅读(334)
推荐(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
阅读(250)
推荐(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
阅读(187)
推荐(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
阅读(205)
推荐(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
阅读(176)
推荐(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
阅读(268)
推荐(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
阅读(282)
推荐(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
阅读(278)
推荐(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
阅读(141)
推荐(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
阅读(142)
推荐(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
阅读(161)
推荐(0)
HDU4515 小Q系列故事——世界上最遥远的距离
摘要:题目链接。分析:原来一天天数是这样简单。。#include <stdio.h>int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int is_leap(int n){ return ((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0));}void pre(int n){ int y = 2013, m = 3, d = 24; while(n--){ d--; if(d == 0){ m--; ...
阅读全文
posted @
2013-03-31 15:56
Still_Raining
阅读(183)
推荐(0)
ZOJ2972 Hurdles of 110m(dp)
摘要:题目链接。分析:这题是几周前比赛时没有做出来的一道。说来惭愧,比完赛,翻翻解题报告也没看懂,就把这题放下了。今天闲来没事,做了做,一遍AC。果然应了那句古话,岁月不饶人(题)啊。设dp[i][j]表示第i个阶段j能量所耗费的时间状态转移方程:dp[i][j] = min(dp[i][j], dp[i-1][j-F1]+T1) (Fast Mode)dp[i][j] = min(dp[i][j], dp[i-1][j]+T2) (Normal Mode)dp[i][j] = min(dp[i][j], dp[i-1][j+F2]+T3) (Slow Mode)AC代码如下:#in...
阅读全文
posted @
2013-03-30 21:33
Still_Raining
阅读(233)
推荐(0)
ZOJ1700 Falling Leaves
摘要:题目链接。分析:题目一开始说了一大堆,以为没用,就没看。然后题不会做。后来把题目仔细看了一遍。。恍然大悟。这题的树好像叫二叉搜索树(表示木听过)。。逆序见树。#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct node{ char ch; struct node *left, *right;}node;void del(node *T){ if(T){ if(T->left) del(T->left); if(T->right) del(T->r
阅读全文
posted @
2013-03-30 19:59
Still_Raining
阅读(326)
推荐(1)