随笔分类 -
DP
HDU 2323
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2323把六边形抽象成坐标进行dp,抽象出的坐标关系必须满足六边形之间的关系。很有趣的一道dp#include using namespace std ;int dp[25][25][25] ;int main(){ dp[0][7][7]=1 ; for(int i=1 ;i<=14 ;i++) { for(int j=0 ;j<=14 ;j++) { for(int k=0 ;k<=14 ;k++) dp[i]...
阅读全文
卡特兰数
摘要:模型:一个凸n边形,用n-3条不相交的对角线把它分成n-2个三角形,求不同的方法数目边界为f(2)=f(3)=1,第四项开始为2、5、14、42、132、429、1430、4862、16796递推公式:f(n+1)=(4*n-6)*f(n)/n
阅读全文
数位dp
摘要:http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html写得够好了个人习惯从1计数。。无伤大雅。。dp数组最开始memset成-1一次就够了int dfs(int i,int s,int e){ if(!i)return s==target_s ; if(!e && dp[i][s]!=-1)return dp[i][s] ; int u=e?digit[i]:9 ; int res=0 ; for(int d=0 ;d<=u ;d++) res+=dfs(i-1,new_s,e &&...
阅读全文
HDU 数位dp
摘要:模板http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html完全理解以后,我发现这种写法实在是太厉害了,简洁,优美,可以回避很多细节问题,而这些细节如果用递推的方法写,处理起来可能会非常痛苦http://acm.hdu.edu.cn/showproblem.php?pid=2089不要62http://www.cnblogs.com/xiaohongmao/p/3473599.html前几天写过这道题的解题报告,两种解法都有http://acm.hdu.edu.cn/showproblem.php?pid=3555不要49#in
阅读全文
HDU 1806
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1806非常玄妙的rmq问题,这个st算法有点神#include #include using namespace std ;int dp[100005][20] ;int a[100005],b[100005] ;void makermq(int n,int *tt){ for(int i=0 ;i>1 ; if(a[mid]>temp) r=mid-1 ; else if(a[mid]=0 ;i--) { if(i=...
阅读全文
HDU 2089
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2089基础的数位dp,当初不懂数位dp水过去的,今天重新写一下,解释看注释预处理+递推学自http://wenku.baidu.com/view/9de41d51168884868662d623.html#include using namespace std ;int f[8][10] ;//f[i][j]表示第i位是数j时符合条件的数字数量 int digit[9] ;//digit[i]表示n从右到左第i位是多少 void Init(){ f[0][0]=1 ; for(int i=1 ;...
阅读全文
LCS
摘要:最长公共子序列的记忆化搜索模板a,b数组分别存两个字符串,dp数组初始化为-1s1表示a串起始地址,e1表示a串结束地址+1,s2、e2同理表示b串int LCS(int s1,int e1,int s2,int e2){ if(dp[s1][s2]!=-1) return dp[s1][s2] ; if(s1==e1 || s2==e2) return dp[s1][s2]=0 ; if(a[s1]==b[s2]) return dp[s1][s2]=1+LCS(s1+1,e1,s2+1,e2) ; else ...
阅读全文
HDU 1712 ACboy needs your help
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1712赤裸裸的分组背包View Code #include <iostream>using namespace std ;int dp[101] ;int val[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<=m;j++) scanf("%d",&val[i]
阅读全文
HDU 2159 FATE
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2159二维费用的背包wa了好多次,要求最大忍耐度,开始没处理好,想当然了View Code #include <iostream>using namespace std ;int dp[101][101] ;int w[101],c[101] ;int main(){ int n,m,k,s ; while(~scanf("%d%d%d%d",&n,&m,&k,&s)) { for(int i=0;i<k;i++) scanf("
阅读全文
HDU 1757 A Simple Math Problem
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1757还是矩阵+快速幂,注意要把初值乘回去并且注意方向View Code #include using namespace std ;void mul(int a[11][11],int b[11][11],int...
阅读全文
HDU 1575 Tr A
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1575矩阵+快速幂A^k是A*A*A...(k个A相乘)View Code #include using namespace std ;void mul(int a[11][11],int b[11][11],in...
阅读全文
HDU 2065 "红色病毒"问题
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2065dp转移方程:dp[i][1]=2*dp[i-1][1]+dp[i-1][2]+dp[i-1][3];dp[i][2]=dp[i-1][1]+2*dp[i-1][2]+dp[i-1][4];dp[i][3]=...
阅读全文
HDU 1005 Number Sequence
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1005神奇的矩阵View Code #include <iostream>using namespace std ;void mul(int a[2][2],int b[2][2]){ int c[2][2] ; c[0][0]=(a[0][0]*b[0][0]+a[0][1]*b[1][0])%7 ; c[0][1]=(a[0][0]*b[0][1]+a[0][1]*b[1][1])%7 ; c[1][0]=(a[1][0]*b[0][0]+a[1][1]*b[1][0])%7 ; ...
阅读全文
HDU 2686 Matrix
摘要:http://acm.bjtu.edu.cn/vjudge/problem/viewProblem.action?id=1815经典的双线程dp,dp[i][x1][y1][x2][y2]表示走i步在(x1,y1),(x2,y2)两点处取得的和的最大值,假设矩阵从0-n-1,有i=x+y,五维转化成三维。代码中用了滚动数组,不过在这里意义不大View Code #include <iostream>using namespace std ;int map[31][31],dp[3][31][31] ;int MAX(int a,int b,int c,int d){ a=max(a
阅读全文
HDU 1520 Anniversary party
摘要:http://acm.bjtu.edu.cn/vjudge/problem/viewProblem.action?id=669最最基础的树形dp,父子兄弟结构太爽了,学自hh博客View Code #include <iostream>using namespace std ;struct Tree{ int father ; int child ; int brother ; int happy ; int temp ; int MAX(){ return max(happy,temp) ; } void init(){ ...
阅读全文
HDU 1114 Piggy-Bank
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1114完全背包,求最小值View Code #include <iostream>using namespace std ;int V ;int dp[1000001] ;const int INF=0xfffffff ;void CompletePack(int c,int w){ for(int i=c;i<=V;i++) dp[i]=min(dp[i],dp[i-c]+w) ; return ;}int c[50001],w[50001] ;int main(){ in...
阅读全文
HDU 2844 Coins
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2844求的是能买多少种价钱的物品,多重背包教主很经典的男人八题之一。。。不过据说原题要用单调队列优化,这个二进制优化就过了。。。View Code #include <iostream>using namespace std ;int V ;int dp[100001] ;void ZeroOnePack(int c,int w){ for(int i=V;i>=c;i--) dp[i]=max(dp[i],dp[i-c]+w) ; return ;}void CompletePac...
阅读全文
HDU 1059 Dividing
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1059基础的二进制优化的多重背包View Code #include <iostream>using namespace std ;int V ;int dp[200001] ;void ZeroOnePack(int c,int w){ for(int i=V;i>=c;i--) dp[i]=max(dp[i],dp[i-c]+w) ; return ;}void CompletePack(int c,int w){ for(int i=c;i<=V;i++) ...
阅读全文
HDU 4314 Save the dwarfs
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4314dp,dp[i][j]表示i个小矮人能逃出去j个时需要之前井中剩下的人的最小a高度之和转移方程dp[i][j]=min(dp[i-1][j]-kk[i-1].a,max(dp[i-1][j-1],h-sumA[i-1]-kk[i-1].b))最神的是解题报告直接把a+b排了个序(a+b最小的先逃脱),这个自己没看出来。。。View Code #include <iostream>#include <algorithm>using namespace std ;const int
阅读全文
01,完全,多重背包
摘要:View Code void ZeroOnePack(int c,int w){ for(int i=V;i>=c;i--) dp[i]=max(dp[i],dp[i-c]+w) ; return ;}void CompletePack(int c,int w){ for(int i=c;i<=V;i++) dp[i]=max(dp[i],dp[i-c]+w) ; return ;}void MultiplePack(int c,int w,int a){ if(c*a>=V) { CompletePack(c,w)...
阅读全文
|
|