随笔分类 -  DP

摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1054题意:在一个树中有一些结点,若在一个结点占一个士兵,则和这一节点相邻的结点被控制(包括此节点),求最少需要多少个士兵;思路:用树形dp,num[i][2]数组存储这个结点存在和不存在士兵的情况下子节点所需士兵的最少数目;code:View Code #include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;struct node 阅读全文
posted @ 2012-04-23 20:55 LT-blogs 阅读(210) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=3184题意:移动奶牛的位置,使相邻两头奶牛的距离满足D或D+1,要注意第一头奶牛在第一个位置,最后一头在最后一个位置,求奶牛的移动步数;思路:滚动数组来记录从第一头奶牛开始所在位置的最小移动步数;代码:View Code #include <cstdio>#include <algorithm>#include <iostream>#include <cstring>using namespace std;#define max 9999999int a[10010] = {0};int s[ 阅读全文
posted @ 2012-04-12 21:24 LT-blogs 阅读(474) 评论(0) 推荐(0)
摘要:http://acm.tju.edu.cn/toj/showp3017.htmlhttp://acm.sdut.edu.cn/web/problem.php?action=showproblem&problemid=2384思路:dp题,自己脑子太笨了,代码如下,提醒一下自己;View Code #include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;int s[110] = {0};int n = 0;int m 阅读全文
posted @ 2012-03-04 20:42 LT-blogs 阅读(176) 评论(0) 推荐(0)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2196题意:在一个每条边都有权值的树中,求每个顶点能走的最大距离;思路:两次dfs,以1为根结点,一次从叶子到根结点,求出最远距离和次远距离。另一次从根进行dfs.(res[]为经过父节点能走最长距离)代码:View Code #include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#define sum 10010using 阅读全文
posted @ 2012-02-21 21:56 LT-blogs 阅读(146) 评论(0) 推荐(0)
摘要:http://acm.timus.ru/problem.aspx?space=1&num=1353题意:输入一个s,求从1-10^9的数中各位数字之和是s的个数;思路:采用一个0-81的数组记录和为i的个数,然后对每一位都从0-9进行遍历;View Code #include<cstdio>using namespace std;int main(){ int ans[100],temp[100],n,i,j,k; for(i=0;i<100;i++) ans[i]=temp[i]=0; scanf("%d",&n); if(n==1) p 阅读全文
posted @ 2011-12-10 09:50 LT-blogs 阅读(281) 评论(0) 推荐(0)
摘要:http://acm.timus.ru/problem.aspx?space=1&num=1081无语了一个简单题花了一个下午才调出来,悲剧啊;题意:给定一个有N个元素的字符串,每个元素都是0或1,且1和1不能相连,问第n个字符串是什么;思路;首先是一个斐波那契数列,然后往下减输出;View Code #include<cstdio>#include<cstring>using namespace std;int s[50];void pp(int i,int num){ if(i<1) return ; if(i<=1) { if(num==1|| 阅读全文
posted @ 2011-12-08 18:46 LT-blogs 阅读(174) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2264题意:找出一个包含两个字串的最短的串;思路:先找出两个字符串的LCS,然后构造目标字符串。例如,有两个字符串 apple pffeach 这两个字符串的LCS是P E 那么分别找出P,E在两个字符串的位置,再将不属于LCS的字符插进来,就变成了: (a) P (pl) (ff) E (ach)View Code #include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main(){ char a[105] 阅读全文
posted @ 2011-12-02 21:01 LT-blogs 阅读(331) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=1159题意:给一个长度为n的串,求将其变成回文要增加的最少字符数.思路:LCS算法:状态方程:if(a[i]==b[j]) ans[i][j]=max1(ans[i-1][j-1]+1,ans[i-1][j],ans[i][j-1]); else ans[i][j]=max(ans[i-1][j],ans[i][j-1]);二维数组的形式(必须用short,否则超内存):View Code #include<cstdio>#include<algorithm>#include<cstring>using 阅读全文
posted @ 2011-12-02 18:48 LT-blogs 阅读(160) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=1836题意:从一列士兵中剔除一些,使剩下的符合a1>a2>a3>...>ai ai+1<ai+2<....<aN-1<aN思路:用了两个LIS分别从前往后和从后往前,得到啊a[],b[],最后取a[i]+b[i]的最大值;View Code #include<cstdio>#include<cstring>#include<algorithm>using namespace std;double s[1010];int n,a[1010],b[1010],i 阅读全文
posted @ 2011-11-29 18:58 LT-blogs 阅读(162) 评论(0) 推荐(0)
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1025题意:题目的意思就是有两种城市,穷和富,要富的运到穷的里面问你最多能建几条路。思路:就是按穷的递增序列来找富的城市里的最大上升子序列。就是LIS法,因为有500000个数据所以用普通的方法肯定超时,那么就用二分查找就可以了。每次就是找第一个比这个数大的数如果没有就放在最右边把最长的序列加一,否则把第一个比他大的数给替换掉。View Code #include<cstdio>using namespace std;int n;int s[500010],ans[500010],a,b;int 阅读全文
posted @ 2011-11-29 14:05 LT-blogs 阅读(187) 评论(0) 推荐(0)