随笔分类 - 动态规划
黑书 刘汝佳 DP
摘要:const int MM = 1000+5;#define debug puts("wrong") #define inf 1000000 #define mod 1000000007 int N,M,K, L;int g=1; void printcase() {printf("Case %d: ",g++);}struct Point{ int x,y,r; bool friend operator<(Point a,Point b) { return a.r<b.r; } }num1[MM],num2[MM];Point start,
阅读全文
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4317有可能error串包含相互的error串,所以建立fail的时候val的标记也要相互转移。View Code typedef long long ll;ll N;#define mod 1000000009 #define debug puts("wrong");#define ROOT 0const int MAX_NODE=2222;//stringnum*stringlenconst int CHI=2; //儿子数ll dp[222][MAX
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1545View Code const int MM = 11111;#define debug puts("wrong")typedef __int64 int64;int N,M;int64 dp[63][11][11];//j min(0-1) k max(0-1)void solve() { int i,j,k;int64 tmp,t1,t2; memset(dp[0],0,sizeof(dp[0])); dp[0][M][M]=1; for(i=1;i<=N;i++) { .
阅读全文
摘要:http://poj.org/problem?id=2663View Code //放骨牌类问题,dfs枚举方法,递推。 const int MM = 1111;#define debug puts("wrong")typedef __int64 int64;int64 N,M,dp[111][11];void dfs(int64 row,int64 col,int64 pre,int64 now,int64 ss) { if(col>=3) { if(pre==7) dp[row][now]+=ss; return; } if((pre&(1<<
阅读全文
摘要:http://acm.fzu.edu.cn/problem.php?pid=2113枚举1的个数,ans=ans+i*dfs();View Code const int MM = 11111;typedef __int64 int64;#define debug puts("wrong")int cnt, num[MM];int64 dp[20][20][20],L,R;int64 dfs(int le,int sum,int s,bool less) { if(le==-1) return sum==s; if(!less && dp[le][sum][s
阅读全文
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3416注意当0也是满足条件的情况下, 假设数字分解有cnt位,则结果将会多出cnt-1个0,即ans-cnt+1为最后结果。View Code
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4352类似于O(nlog n)的LIS求法,用1<<10记录10以内数的出现情况,贪心更新。View Code //类似于O(nlog n)的LIS求法,用1<<10记录10以内数的出现情况,贪心更新。const int MM = 11111;typedef __int64 int64;int K,cnt, num[MM];int64 dp[20][1<<10][10],L,R;void get_data() { scanf("%I64d%I64d%d"
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3652View Code //dp[i][j][3] //表示前i个字符,余数为j //0 存在13,1前一个是1 ,2什么都没有const int MM = 1111;typedef __int64 int64;#define debug puts("wrong")#define mod 13int N,M;int num[MM], cnt;int dp[11][15][3]; //0 have 13 // 1 pre=1 // 2 noint dfs(int le,int r,int
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4389View Code const int MM = 1111;typedef __int64 int64;#define debug puts("wrong")int N,M;int num[MM], cnt;int dp[10][82][82][82];int dfs(int le,int sum,int mod,int r,bool less) { //less前一位是否取到最大e=less?num[le]:9 if(sum>mod) return 0; if(le==-1)
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3507View Code const int MM = 555555;#define debug puts("wrong")#define mod 100000000typedef __int64 int64;int N,M;int val[MM],sum[MM], dp[MM];int head,tail,sq[MM];/***************\斜率优化DP dp[i]=min(dp[j]+sqr(sum[i]-sum[j])+M | 1<=j<i)对于队尾a b ,
阅读全文
摘要:http://acm.fzu.edu.cn/problem.php?pid=2107状态压缩DP,dp[i][j][k] 表示前i行j状态,有没有2*2普通放板的问题。深搜枚举的过程中记录当前和转移到的状态。View Code const int MM = 1111;typedef __int64 int64;#define debug puts("wrong")int N,M,L,ans,ss;char str[MM];int num[MM];int dp[10][32][3]; //0 no 1 yesvoid get_data() { int i,j,k; scanf
阅读全文
摘要://组合数 com(n,r)View Code /*==========================*\| 组合数 com(n,r)\*==========================*/const int maxn = 100; //元素个数int com[maxn][maxn]; //注意 long longvoid get_com() { int i,j,k; for(i=0;i<maxn;i++) { com[i][0]=1; for(j=1;j<=i;j++) com[i][j]=com[i-1][j-1]+com[i-1][j]; ...
阅读全文
摘要:http://poj.org/problem?id=1699状态压缩,预处理出任意两个串的最长前缀和后缀的公共部分,然后DP。View Code //POJ1699const int MM = 110;#define maxint 0x3f3f3f3fint N;int d[1<<11][11];char str[MM];int len[MM][MM];int fail[MM];char ch[21][MM];int cal(int s1,int s2) { int i,j,k,n,m,ans=0; n=strlen(ch[s1]+1); m=strlen(ch[s2]+1)...
阅读全文
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4778优先队列优化下的DP,dp[i]记录的是吃到i天的最小花费,最后dp[N]即为答案。View Code const int MM = 111111;#define debug puts("wrong")typedef long long int64;//typedef __int64 int64;int64 N, cnt;struct Info{int64 x,y;}p[MM];struct Tpoint{ int64 val,dead; //Tpo
阅读全文
摘要:http://acm.timus.ru/problem.aspx?space=1&num=1057View Code const int MM = 22222;int64 L, R, K, B, len;int64 C[33][33];int64 p[111];void get_p() { int64 i,j,k; p[0]=1; len=(int64)((log(1.0*R)/log(1.0*B))+1.0);// printf("%d\n",len); for(i=1;i<=len;i++) p[i]=p[i-1]*B;// for(i=0;i<=l
阅读全文
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=524LCS 细节View Code #include <iostream>#include <cstdio>#include <vector>#include <cmath>#include <set>#include <map>#include <queue>#include <string>#include <cstring>#include <algorit
阅读全文
摘要:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2901一般DP复杂度 O(L*N^2), 分段计算 O(lgL*N*N)View Code #include <iostream>#include <cstdio>#include <vector>#include <queue>#include <map>#include <string>#include <cstring>#include <algorithm>int f_
阅读全文
摘要:http://acm.buaa.edu.cn/problem/431/DP 分解质因数,复杂度O(10N)。100000以内的数的质数因子不超过10个。转化为记录质因数个数的最大值。View Code const int MM = 22222;const int PP = 100005;int N, ans;bool isp[PP];int p[PP],cnt;int pt[PP],cc;int dp[PP];int num[MM];void get_p() { int i,j,k; memset(isp,true,sizeof(isp)); isp[0]=isp[1]=fa...
阅读全文
摘要:View Code 1 Remember the three steps!2 Defining subproblems3 Finding recurrences4 Solving the base cases
阅读全文
摘要:Traveling Salesman ProblemSolve:暴力解法复杂度为O(N!)这个是难以承受的,可以用朴素的DP划分思想达到O(N^2*2^N)。N个节点有2^N个子集,记录集合中以v为结束点的集合个数为N*2^N个。Best Sequence预处理出每个字符串和其他串的最大匹配长度,跑一遍 TSP。code:View Code #include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <cstring>using n
阅读全文