摘要:地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1047题意:大数乘法代码:#include <stdio.h>#include <string.h>#include <stdlib.h>char a[260],b[260];int x[260], y[260], mut[610];int main(){ while(~scanf("%s%s", a, b)) { me
阅读全文
摘要:地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=365题意:求大数a+b代码:#include <stdio.h>#include <string.h>#include <stdlib.h>char a[200], sum[200];int main(){ int le,i,j,k; memset(sum, '0', sizeof(sum))
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1010题意:迷宫,是否能恰好t步走到终点。mark:奇偶剪枝法!!!这篇博客写的不错http://www.cppblog.com/Geek/archive/2010/04/26/113615.html代码:#include <stdio.h>#include <string.h>#include <stdlib.h>int n,m,t,sum,flag;char a[10][10];int s1,s2,d1,d2;int tab[4][2] = {1, 0, -1,
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1059题意:有价值为1,2,……,6的大理石各若干个,求是否能平分给两个人。mark:看别人说多重背包,一直不知道在说什么。后来终于懂了,把总价值平分,往背包里放东西,如果最后恰好等于总价值的一半,那么就可以放进去。 这个代码就当模版用了。。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>int a[7], vmax;int dp[120010];int max(int a, int b) {ret
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1080题意:匹配两个人相似度。A,G,C,T,每两个都会有一个对应的值,给出两串基因,长度可以不一样,可以在基因中间加_使两串长度一样,然后有一个对应值,求最大对应值。mark:LCS的升级版。dp[i][j] = max(dp[i-1][j]+tab[s[0][i-1]][4], dp[i][j-1]+tab[s[1][j-1]][4], dp[i-1][j-1]+tab[s[0][i-1]][s[1][j-1]]);代码:#include <stdio.h>#include <st
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1428题意:问最短路径有多少条。mark:首先bfs是用来确定每个点到终点的最短路径。dfs来确定有多少条相同的最短路径。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>#include <queue>#define LL long longusing namespace std;typedef struct{ int a,b;}qq;const int N = 60;const int
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1208题意:走迷宫,每个格子里面数字代表与下一个格子间隔是几,求有多少种走法到n。mark:记忆化搜索。本来觉得直接暴力搜索可过,结果tle了。dp[i][j]存放该格子有多少总走法。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>int n;char a[40][40];int s[40][40];long long dp[40][40];int tab[2][2] = {1, 0, 0, 1};
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1078题意:老鼠偷吃,有n*n的方阵,每个格子里面放着一定数目的粮食,老鼠每次只能水平或竖直最多走k步,每次必须走食物比当前多的格子,问最多吃多少食物。mark:记忆化搜索。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>int n,m,max1;int s[110][110],dp[110][110];int tab[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};int m
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1074题意:有很多作业,没有在规定时间内完成要扣分,一天一分,求最小扣分!mark:wa了两次,傻了。。把题意理解错了。是超过期限一天就扣一分!!! 状态压缩dp。dp[i]代表第i个状态的最小罚时,由于本题要输出顺序,所以给dp加上了一个变量,是该状态是由哪个状态来的。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>const int M = (1 << 15);typedef str
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=4374题意:有n层,每层有m个模块,每个模块都有对应的权值,每层只能沿一个方向走,每层最多走t步,求最大权值的和。mark:单调队列,dp[i][j]代表第i层,第j块最大的权值。 首先每次求得dp[i][j]后,先向下走给dp[i+1][j]一个初始值,即dp[i+1][j] = dp[i][j]+w[i+1][j],(w[i][j]表示第i层第j块的权值。) 然后单调队列,左边右边各弄一次,去最大值。代码:#include <stdio.h>#include <string.h&g
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=4362题意:挖宝石,宝石都出现在一条直线上,每次出现n个宝石,只能挖其中一个,剩下消失。总共有m次机会。精力消耗有路程的消耗和挖宝石的时候消耗,求最小消耗。mark:dp。维护两个最值,把问题分成从位置大于和小于目前位置两种情况讨论。dp[i][j]代表第i段时间挖第j个石头消耗最小。 首先只考虑位置比目前小的情况,dp[i][j] = min{dp[i-1][k]+p[i][j]-p[i-1][k]+e[i][j]} = min{dp[i-1][k]-p[i-1][k]}+p[i][j]+e[i]..
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=4340题意:两个人抢劫一个城市,每个人抢前一个城市相邻的城市的时候只用花费1/2的时间,求最短时间。mark:树状dp。dp[i][j][k]代表第i个城市由第j个人抢劫,第i个城市所属子树需要完全时间的个数。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>const int M = 105;const int N = 10000000;int n,s[2][M];int dp[M][2][2];b
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=3415题意:给一个序列,规定区间大小,求和最大的子序列。mark:单调队列。。。经验不足,做了很久。代码:#include <stdio.h>#include <string.h>const int M = 100010;int sum[2*M], a[2*M], q[2*M];int main(){// freopen("in.txt", "r", stdin);// freopen("out.txt", "
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=3401题意:炒股……规定每天最多买进多少,卖出多少,并且每次炒股后隔w天才能再次炒股,问最大收益。mark:首先想到最朴素的dp。dp[i][j]代表第i天有j股会带来最大收益。 则dp[i][j] = max(dp[i-1][j], dp[r][k]-(j-k)ap[i], dp[r][k]+(k-j)bp[i]); 复杂度是O(T*T*Maxp*Maxp)。 首先想到第一步优化,前面式子r范围是0~i-w-1,怎么优化呢,因为dp[i][j]至少为dp[i-1][j],那么dp[i-w-1...
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=3001题意:n(最多10)个城市,可以从任意城市出发,每个城市最多走两次,问最短路径。mark:TSP问题,由于最多走两次,所以是三进制状态压缩DP,第一次写,很艰难。 可以转换成任意进制的~代码:#include <stdio.h>#include <string.h>#include <stdlib.h>const int M = 67149;const int inf = 0x3f3f3f3f;int dp[70000][15];int adj[15][15];
阅读全文
摘要:地址:http://poj.org/problem?id=2823题意:n个数里面连续的k个数,找最大和最小的。mark:优先队列,不过要重载一下cmp函数。 也可以单调队列做。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>int queue[1000010];int a[1000010];int m,n;void solve(int f){ int i,j,k; int fr,la; fr = la = 0; queue[la++] = 0; for(i = 1; i < m;
阅读全文
摘要:地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3471题意:n种气体,每两种碰在一起会产生能量,然后后者消失。自己不能跟自己撞。求产生的最大能量。mark:状态压缩dp。自己写的时候很忐忑,最后看解体报告发现跟自己思路一样,嘿嘿~代码:#include <stdio.h>#include <string.h>#include <stdlib.h>#define J1 (k|(1<<j))#define J2 (k&(1<<j))#define I
阅读全文
摘要:地址:http://poj.org/problem?id=1753题意:黑白棋,翻动一个格子,周围相连的格子都翻转,问最少几次使所有棋子颜色相同。mark:正解高斯消元,第一次写,写了两天,终于理解并ac了~代码:#include <stdio.h>#include <string.h>int g[20][20];int min(int a, int b) {return a < b ? a : b;}int find(int i){ int j; for(j = i; j < 17; j++) if(g[j][i]) return j; return 0;
阅读全文