随笔分类 -  DP

 
HDU 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2191裸多重背包View Code #include <iostream>#include <algorithm>using namespace std ;int dp[10001] ;int c[101],w[101],num[101] ;int V ;bool cmp(int a,int b){ return a>b ;}void ZeroOnePack(int c,int w){ for(int i=V;i>=c;i--) dp[i]=max(dp[i],dp[i-c 阅读全文
posted @ 2012-07-26 11:05 LegendaryAC 阅读(241) 评论(0) 推荐(0)
HDU 1171 Big Event in HDU
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1171多重背包,学自背包九讲。re一次,dp数组开多大算起来太纠结,尽可能开大就不会有事了wa一次,dp数组没有memset。。。View Code #include <iostream>using namespace std ;int dp[1000001] ;int v[51],num[101] ;int V ;void ZeroOnePack(int c,int w){ for(int i=V;i>=c;i--) dp[i]=max(dp[i],dp[i-c]+w) ; ret... 阅读全文
posted @ 2012-07-26 10:44 LegendaryAC 阅读(156) 评论(0) 推荐(0)
HDU 2577 How to Type
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2577大小写打字,注意shift的使用,注意初始化View Code #include <iostream>using namespace std ;int dp[101][3] ;int main(){ int t ; scanf("%d",&t) ; while(t--) { char s[101] ; scanf("%s",s+1) ; int len=strlen(s+1) ; memset(dp,0,sizeof(dp)... 阅读全文
posted @ 2012-07-23 22:03 LegendaryAC 阅读(133) 评论(0) 推荐(0)
POJ 1949 Chores
摘要:http://poj.org/problem?id=1949一句话破题,“Farmer John's list of chores is nicely ordered, and chore K (K > 1) can have only chores 1,.K-1 as prerequisites.”View Code #include <iostream>using namespace std ;int dp[10001] ;int main(){ int n ; while(~scanf("%d",&n)) { int w,k,maxx 阅读全文
posted @ 2012-07-23 22:01 LegendaryAC 阅读(189) 评论(0) 推荐(0)
POJ 3230 Travel
摘要:http://poj.org/problem?id=3230dp[i][j]表示第i天在城市j取得的最大价值这题要注意初始化,最大价值可能是负的。View Code #include <iostream>using namespace std ;int cost[101][101],income[101][101] ;int dp[101][101] ;int main(){ int n,m ; while(scanf("%d%d",&n,&m),n||m) { for(int i=1;i<=n;i++) for(int j=1;j< 阅读全文
posted @ 2012-07-23 17:19 LegendaryAC 阅读(116) 评论(0) 推荐(0)
HDU 1159 Common Subsequence
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1159dp[i][j]代表长度为i的串s1与长度为j的串s2的最长公共子串View Code #include <iostream>using namespace std ;int dp[1001][1001] ;int main(){ char s1[1001],s2[1001] ; while(~scanf("%s%s",s1,s2)) { int len1=strlen(s1) ; int len2=strlen(s2) ; memset(dp... 阅读全文
posted @ 2012-07-23 15:09 LegendaryAC 阅读(121) 评论(0) 推荐(0)
HDU 1421 搬寝室
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1421因为考虑到差的平方最小,所以先把物品按重量排序(这样可以使相邻的两个数更加接近从而取得更优的结果)。dp[i][j]表示i件物品拿j对时最低的疲劳度i等于2*j时,dp[i][j]=dp[i-2][j-1]+(w[i]-w[i-1])^2其他情况时(即i大于2*j),dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(w[i]-w[i-1])^2)View Code #include <iostream>#include <algorithm>using 阅读全文
posted @ 2012-07-23 09:43 LegendaryAC 阅读(427) 评论(0) 推荐(0)
HDU 2151 Worm
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2151水View Code #include <stdio.h>#include <string.h>int n,p,m,t ;int dp[110][110] ;int main(){ while(~scanf("%d%d%d%d",&n,&p,&m,&t)) { memset(dp,0,sizeof(dp)) ; dp[0][p]=1 ; for(int i=0;i<=m;i++) for(int j=1;j<=n;j 阅读全文
posted @ 2012-06-10 19:35 LegendaryAC 阅读(139) 评论(0) 推荐(0)
LIS
摘要:O(nlog(n))的算法,网上讲解有很多,我就不在这里献丑了,直接上模板该模板计算从1到n的LIS,p[]为存放数列的数组最长上升子序列View Code int LIS(int n){ int l,r,m,i,tail = 0; for ( dp[ ++ tail ] = p[ 1 ... 阅读全文
posted @ 2012-05-29 12:28 LegendaryAC 阅读(367) 评论(0) 推荐(0)
HDU 1677 Nested Dolls
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1677这道题纠结了N个小时,因为原来找的模板里少了两个等号。。。结果就是错在了我一直觉得不会错的LIS里,让我认清了上升和非降的区别?下降数列的个数等于LIS,和导弹拦截那道题基本一致ps:连着三道LIS看排名都进了前三,好开心~View Code #include <iostream>#include <algorithm>using namespace std ;int dp[10001],p[20001];typedef struct L{ int w,h;}L;L kk[200 阅读全文
posted @ 2012-05-29 12:07 LegendaryAC 阅读(464) 评论(0) 推荐(0)
HDU 1950 Bridging signals
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1950和上一题基本一样,二分dp求LISps:排名居然刷到第一了,原来从来没有遇到呢,好开心~View Code #include <iostream>using namespace std ;int dp[10001],p[40001];int LIS(int n){ int l,r,m,i,tail = 0; for ( dp[ ++ tail ] = p[ 1 ],i = 2 ; i <= n ; ++ i ) { if ( dp[ tail ] <= p[ i ] )... 阅读全文
posted @ 2012-05-28 21:37 LegendaryAC 阅读(198) 评论(0) 推荐(0)
HDU 1025 Constructing Roads In JGShining's Kingdom
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1025题目有一定迷惑性,实际就是求LIS,我原来掌握的朴实的O(n^2)算法果断超时,新学了一种二分dp O(nlog(n))的算法,直接上模板了,还要多多体会啊View Code #include <iostream>using namespace std ;int dp[10001],p[50001];int LIS(int n){ int l,r,m,i,tail = 0; for ( dp[ ++ tail ] = p[ 1 ],i = 2 ; i <= n ; ++ i ) { . 阅读全文
posted @ 2012-05-28 21:29 LegendaryAC 阅读(124) 评论(0) 推荐(0)
HDU 1203 I NEED A OFFER!
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=120301背包ps:做01背包经常不注意数组的大小,这题数组开小了又是各种waView Code #include <iostream>using namespace std ;double f[10001],w[1001];int c[1001];int main(){ int n,m; while(scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; for(int i=1;i<=m;i++) scan 阅读全文
posted @ 2012-05-27 23:16 LegendaryAC 阅读(150) 评论(0) 推荐(0)
HDU 2546 饭卡
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=254601背包,开始内层循环写成v>=0无限wa。。。一定要注意数组越界的问题、、、View Code #include <iostream>using namespace std ;int main(){ int n; while(scanf("%d",&n),n) { int w[1010]; int maxx=0; int flag; for(int i=1;i<=n;i++) { scanf(... 阅读全文
posted @ 2012-05-27 22:38 LegendaryAC 阅读(196) 评论(0) 推荐(0)
HDU 2391 Filthy Rich
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2391一路逆着推过去View Code #include <stdio.h>#include <string.h>const int INF=100000000;int map[1001][1001],dp[1001][1001];int Max(int a,int b,int c){ int max=-INF; if(a>max)max=a; if(b>max)max=b; if(c>max)max=c; return max;}int main(){ int t. 阅读全文
posted @ 2012-05-25 04:04 LegendaryAC 阅读(224) 评论(0) 推荐(0)
HDU 1992 Tiling a Grid With Dominoes
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1992hdu上很多这种砖块问题,废话少说,下面开始分析。高度是固定的,只用考虑宽度带来的影响。少一列,是dp[1]*dp[i-1]=dp[i-1]少两列,是dp[2]*dp[i-2]=5*dp[i-2],但是其中一种情况与少一列的重复,所以只有4*dp[i-2]再往后少奇数列,都只有2*dp[i](考虑可能情况并且不与前两种情况产生重复)再往后少偶数列,都只有3*dp[i](考虑可能情况并且不与前两种情况产生重复)综上分析,dp[i]=dp[i-1]+4*dp[i-2]+2*(dp[i-3]+dp[i-5]. 阅读全文
posted @ 2012-05-02 12:44 LegendaryAC 阅读(653) 评论(0) 推荐(0)
HDU 1176 免费馅饼
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1176类似数塔的dp,注意边界处理!!!View Code #include <stdio.h>#include <string.h>int dp[110000][30];int max3(int a,int b,int c){ int maxnum=a; if(maxnum<b)maxnum=b; if(maxnum<c)maxnum=c; return maxnum;}int max2(int a,int b){ return a>b?a:b;}int main( 阅读全文
posted @ 2012-04-25 22:12 LegendaryAC 阅读(162) 评论(0) 推荐(0)
HDU 1087 Super Jumping! Jumping! Jumping!
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1087求最长上升子序列问题View Code #include <stdio.h>#include <string.h>#include <stdlib.h>int cmp(const void*a,const void*b){ return *(int*)b-*(int*)a;}int main(){ int n,i,j; int a[1100],dp[1100]; while(scanf("%d",&n),n) { for(i=0;i< 阅读全文
posted @ 2012-04-24 21:30 LegendaryAC 阅读(127) 评论(0) 推荐(0)
HDU 1257 最少拦截系统
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1257和POJ和2533一模一样。。。View Code #include <stdio.h>#include <string.h>#include <stdlib.h>int cmp(const void*a,const void*b){ return *(int*)b-*(int*)a;}int main(){ int n,i,j; int a[1100],dp[1100]; while(~scanf("%d",&n)) { for(i=0; 阅读全文
posted @ 2012-04-22 21:14 LegendaryAC 阅读(184) 评论(0) 推荐(0)
POJ 2533 Longest Ordered Subsequence
摘要:http://poj.org/problem?id=2533最长上升子序列,dpView Code #include <stdio.h>#include <string.h>#include <stdlib.h>int cmp(const void*a,const void*b){ return *(int*)b-*(int*)a;}int main(){ int n,i,j; int a[1100],dp[1100]; while(~scanf("%d",&n)) { for(i=0;i<n;i++) scanf(&quo 阅读全文
posted @ 2012-04-22 20:55 LegendaryAC 阅读(165) 评论(0) 推荐(0)