随笔分类 - dp
摘要:http://acm.timus.ru/problem.aspx?space=1&num=1017题意:有n块砖,要求按照严格递增的个数摆放成楼梯,求楼梯的摆放种类数。思路:状态转移方程:dp[i][j]=sum(dp[i-j][k]), 0 2 #include 3 #define LL lo...
阅读全文
摘要:和1009相同,只是n达到了180位,可以模拟大数加和大数乘,这里用的java中的大数。 1 import java.math.BigInteger; 2 import java.util.Scanner; 3 public class Main { 4 public static void...
阅读全文
摘要:http://acm.timus.ru/problem.aspx?space=1&num=1009题意:将一个n位数转化为合法的K进制数,有多少种情况。合法的K进制数即不含前导0,且任意两个0都不相邻。思路:每一位的情况都分为:小于K且不等于0的情况或等于0的情况,每一位的选择都有前一位决定。dp[...
阅读全文
摘要:http://codeforces.com/problemset/problem/414/BB. Mashmokh and ACMtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM in
阅读全文
摘要:B. Long Pathtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOne day, little Vasya found himself in a maze consisting of(n + 1)rooms, numbered from1to(n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the(
阅读全文
摘要:http://poj.org/problem?id=2533最长上升子序列的两种算法(1) O(n^2) 1 #include 2 #include 3 4 const int N=10010; 5 int dp[N],a[N];//dp[i]表示0~i的最长上升子序列的长度 6 int n; 7 8 int main() 9 {10 int n;11 while(~scanf("%d",&n))12 {13 memset(dp,0,sizeof(dp));14 for (int i = 0; i a[j] && dp[j]+1...
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1159最长公共子序列 1 #include 2 #include 3 #include 4 using namespace std; 5 const int N=1010; 6 char s1[N],s2[N]; 7 int dp[N][N]; 8 9 int main()10 {11 while(~scanf("%s%s",s1,s2))12 {13 int len1 = strlen(s1);14 int len2 = strlen(s2);15 ...
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1003感觉自己的dp弱爆了。。以前做的都忘得差不多了,只能从最基础的再学一遍吧。。求最大连续子段和。要知道一个定理,即最大连续子段和中不可能包含负的子段和。对于序列(....i......j,j+1.....),如果sum(i,j) 2 #include 3 const int N=100010; 4 int a[N]; 5 int main() 6 { 7 int t,n,o = 0; 8 scanf("%d",&t); 9 while(t--)10 {11 ...
阅读全文
摘要:http://poj.org/problem?id=1276 1 #include 2 #include 3 const int N=1000020; 4 #define Max(a,b) (a)>(b)?(a):(b) 5 int dp[N],cash; 6 void ZeroOnePack(int cost)//01背包 7 { 8 for (int i = cash; i >= cost; i--) 9 {10 dp[i] = Max(dp[i],dp[i-cost]+cost);11 }12 }13 void ComplexPack(int ...
阅读全文
摘要:http://poj.org/problem?id=1836求两遍最长上升子序列,顺序求一遍,逆序求一遍。 1 #include 2 #include 3 const int N=1002; 4 int dp1[N],dp2[N]; 5 double a[N]; 6 int main() 7 { 8 int n; 9 scanf("%d",&n);10 for (int i = 1; i a[j])21 {22 if (temp 0; i--)30 {31 int temp = 0...
阅读全文
摘要:http://poj.org/problem?id=1080 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 7 const int INF=1<<28; 8 int score[220][220]; 9 void init()10 {11 score['A']['C']=score['C']['A']=-1;12 score['A']['G']=score['G']['A&
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1025题意:富人路与穷人路都分别有从1到n的n个点,现在要在富人点与穷人点之间修路,但是要求路不能交叉,问最多能修多少条。思路:穷人路是按顺序给的,故求富人路的最长上升子序列即可。由于数据范围太大,应该用O(nlogn)的算法求LIS。 1 #include 2 #include 3 #include 4 using namespace std; 5 const int N=500005; 6 int r[N],d[N]; 7 int main() 8 { 9 int n,cnt = 0,x,y...
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1506题意:给出n个矩形的高度,每个矩形的宽都为1,求相邻的矩形能组合成的最大的矩形的面积。思路:求出比第i个矩形大的最左边的矩形的位置 l[i], 及比第i个矩形大的最右边的矩形的位置 r[i], 则第i个矩形的面积 s = (r[i]-l[i]+1)*hign[i]。 如果第i-1个矩形比第i个矩形大,则 l[i] 必定在 l[i-1]的 左边,同理,r[i]必定在 r[i+1]的右边。 1 #include 2 #include 3 #include 4 #define LL __int64 5 ...
阅读全文
摘要:http://poj.org/problem?id=1260 1 #include 2 #include 3 #include 4 5 using namespace std; 6 int main() 7 { 8 int t,dp[120],s[120]; 9 cin>>t;10 while(t--)11 {12 int n,ai[120],pi[120];13 cin>>n;14 s[0] = 0;15 for (int i = 1; i >ai[i]>>pi[i];18 ...
阅读全文
摘要:http://poj.org/problem?id=3267题意:给出一个主串,一些单词,匹配这些单词,问至少要删除多少字符。 1 #include 2 #include 3 #include 4 #include 5 #include 6 int dp[350];//dp[i]表示i->L删除的字符数 7 using namespace std; 8 int main() 9 {10 int n,L;11 string word[606],s;12 cin>>n>>L;13 cin>>s;14 for (int i = 0; i >word[.
阅读全文
摘要:http://poj.org/problem?id=1159题意:给定一个字符,问最少插入多少字符,使该字符串变成回文字符串。思路:设原字符串序列为X,其逆字符串为Y,则最少插入的字符数=length(X)-X与Y的最长公共子序列的长度。求LCS的状态转移方程为 max(dp[i-1][j],dp[i][j-1]) s1[i-1]!=s2[j-1] dp[i][j] = dp[i-1][j-1]+1; s1[i-1]==s2[j-1];由于数据范围大,本题使用的滚动数组。 1 #include 2 #...
阅读全文
摘要:http://poj.org/problem?id=1837 1 #include 2 #include 3 const int N=15002; 4 int dp[21][N]; 5 int main() 6 { 7 int n,num; 8 int dis[21],w[21]; 9 memset(dp,0,sizeof(dp));10 scanf("%d%d",&n,&num);// n钩子数,num砝码数11 for (int i = 1; i <= n; i ++)12 scanf("%d",&dis[i]);13
阅读全文
摘要:http://poj.org/problem?id=2151看的题解。。表示没看懂状态转移方程。。#include#includeint m,t,n;double dp[1002][32][32],p[1002][32],tt[1002][32];int main(){ int i,j,k; while(~scanf("%d%d%d",&m,&t,&n)) { if (!m && !t && !n) break; memset(dp,0.0,sizeof(dp)); memset(tt,0.0,sizeof(tt...
阅读全文