摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=4281题意:有n个任务等待完成,每个任务有位置,所需时间,每个人最多m分钟。 第一问是求这些任务最少几个人完成;第二问是给无数个人,问最少需要多少分钟完成所有任务,并且每个人上限还是m。mark:第一问可以状态dp,可以01背包记录一下,然后回溯。 第二问参见了大牛的做法,先状态dp,然后暴力搜索一下。 今天才了解到状态压缩dp可以有两种方式写,第一种是从前往后扫一遍,每次转换这个状态能够到达的状态(因为状态肯定是由值小的到值大的) 第二种是用队...
阅读全文
摘要:地址: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://acm.hdu.edu.cn/showproblem.php?pid=1160题意:给出反例,老鼠长的越胖,速度越慢的例子。mark:最长递增子序列。不过本题要求记录这段子序列,不能用传统的nlgn的算法,只能n*n的算法了。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct{ int pos; int w,s;}mice;typedef struct{ int pre; int sum;}dpp;mice m[1010];dpp dp[101
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1494题意:中文……mark:dp[i][j]代表第i个时间段,有j数量的油,油的数量0~14.代码:#include <stdio.h>#include <string.h>#define N 0x3f3f3f3fint dp[2][15];int a[110][2];int min(int a, int b) {return a < b ? a : b;}int main(){ int l,n,min1; int i,j; while(~scanf("%d%d
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1300题意:买珠宝。每种品种必须加上10个同等级珠宝的价钱,可以将低品种的并入高品种,这样可能可以减少价钱。求最小价钱。mark:dp。刚开始思路是小品种的往大品种上加,直到加不了。然后大部分数据可以过,不过显然是wa的。后来看解题报告用新思路做,数据设小了。。wa了。代码:#include <stdio.h>int min(int a, int b) {return a < b ? a : b;}int main(){ int dp[110], a[110][2]; int t, n
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1025题意:上面n个点,下面n个点,然后在这2n个点之间随意连线,一个点只能被连一次,问最多有多少条线不交叉。mark:把上面点排序后,那么就转换成最大升序子序列的问题了。可以看看我上一篇博客~poj 3903代码:#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct{ int s,e;}city;city c[500010];int len;int d[500010];int c
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=1421题意:中文……mark:dp。关键还是找dp[i][j]。i代表前i个物品,j代表多少对。 wa了两次,真2,写顺手了,求了个最大值。。。代码:#include <stdio.h>#include <stdlib.h>#include <string.h>int a[2010];int dp[2010][2010];int cmp(const void *a, const void *b){ return *(int *)a - *(int *)b;}int m
阅读全文
摘要:地址:http://acm.hdu.edu.cn/showproblem.php?pid=2577题意:求字符串最少输入次数。mark:dp,关键是分组!代码:#include <stdio.h>#include <string.h>int min(int a, int b) {return a < b ? a : b;}int dp[110][2];int main(){ int t,i; char a[110]; scanf("%d", &t); while(t--) { scanf("%s", a); mems
阅读全文