随笔分类 -  动态规划

摘要:博客原文地址:http://blog.csdn.net/xuechelingxiao/article/details/38658153Threeprime Numbers题目大意:首先定义一个Threeprime Numbers,Threeprime Numbers的含义就是:对于一个数,这个数中任... 阅读全文
posted @ 2014-08-18 12:27 GLSilence 阅读(261) 评论(0) 推荐(0)
摘要:Football原文链接:http://blog.csdn.net/xuechelingxiao/article/details/38520105大意:2^n 个球队进行单场淘汰赛,每两只球队之间比赛会有胜负的概率,问最后谁夺冠的概率最大。思路:简单的概率DP问题,主要是怎么处理哪两个球队比赛的问题... 阅读全文
posted @ 2014-08-12 20:06 GLSilence 阅读(608) 评论(0) 推荐(0)
摘要:Bicolored Horses大意:给你N匹马,K个马厩,每一个马都只会是0或1,每一个马厩里会有一个不快乐值(不快乐值=0马的个数*1马的个数),问怎么分配会得出一个最小的不快乐值,输出最小的不快乐值。思路:先(n^2)处理出来每个区间中的不快乐值,再用DP求解出K个马厩的最小不快乐值。dp[i... 阅读全文
posted @ 2014-07-29 09:12 GLSilence 阅读(206) 评论(0) 推荐(0)
摘要:Square Country大意:买一块边长为 a 的正方形地需要的钱数是 a^2, 现在输入N为钱的数目,求最少购买地的块数可以凑够N。思路:DP,由背包思想推出来的dp[i] = min(dp[i], dp[j-i*i]+1); 方块都是由正方形组成的,所以是i*i,循环的时候也是i*i。 1 ... 阅读全文
posted @ 2014-07-25 09:52 GLSilence 阅读(149) 评论(0) 推荐(0)
摘要:A nudnik photographer大意:对1到N这些数进行排列,1必须要在最左边,相邻的两个数之间的差值不能超过2,问有多少种排列的方法。思路:对座位进行DP,当第一个是1,第二个是2的时候,组合为dp[i-1];当第一个是1,第二个是3的时候,第三个也确定了是2,组合为dp[i-3];还有... 阅读全文
posted @ 2014-07-24 16:29 GLSilence 阅读(577) 评论(0) 推荐(0)
摘要:Milliard Vasya's Function大意:求1-10^9之间的数中,各数位和为s的数的个数。思路:dp[i][j]表示位数是i的数字各个位之和为j的数的个数(1 2 3 int dp[10][100]; 4 5 int main() 6 { 7 for(int i = 1;... 阅读全文
posted @ 2014-07-24 10:26 GLSilence 阅读(147) 评论(0) 推荐(0)
摘要:Maximum Sum大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少。思路:最开始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4),就不知道该怎么办了。问了一下,是压缩矩阵,转换成最大字段和的问题。压缩行或者列都是可以的。 1 int n, m, x, y, T, t; 2 in... 阅读全文
posted @ 2014-07-23 20:53 GLSilence 阅读(171) 评论(0) 推荐(0)
摘要:Scientific Conference之前一直在刷计算几何,邀请赛连计算几何的毛都买见着,暑假这一段时间就做多校,补多校的题目,刷一下一直薄弱的DP。多校如果有计算几何一定要干掉-。-题意:给你N个报告会的开始时间跟结束时间,问你做多可以听几场报告会。要求报告会之间至少间隔为1。思路:其实是个活... 阅读全文
posted @ 2014-07-23 20:26 GLSilence 阅读(655) 评论(0) 推荐(0)
摘要:HDU 2372大意:给你一个长度为n的数列,求极差小于k的最长的上升数列的长度。思路:DP,循环k,每次求一个最长上升子序列。 1 #include 2 #include 3 #define LL __int64 4 5 int n, m; 6 int a[110]; 7 LL dp[110][110]; 8 9 void Solve()10 {11 while(~scanf("%d%d", &n, &m))12 {13 memset(dp, 0, sizeof(dp));14 if(!n && !m)15 {16 ... 阅读全文
posted @ 2014-04-02 22:04 GLSilence 阅读(220) 评论(0) 推荐(0)
摘要:省赛还有不到50天了,自己DP这块实在是弱,准备就拿着些天狂刷DP了。http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346大意: 求两个字符串的最长公共子序列。思路:水题,不过需要注意的就是字符串里可能会出现空格,需要用gets,真是坑。。。。。 1 #include 2 #include 3 #define max(a, b) a>b?a:b 4 5 char s1[1010], s2[1010]; 6 int dp[ 阅读全文
posted @ 2014-03-25 17:04 GLSilence 阅读(121) 评论(0) 推荐(0)
摘要:Monkey and Banana大意:把给定的长方体(不限个数)叠加在一起,上面的长和宽比下面的长和宽短,求这些长方体加起来的最高高度。思路:每个格子最多3个状态,也就是高最多有3种,也就是一共有N*3 最多90个格子,但是X和Y可以对调,那么就最多180个,对180个格子对X从小到大排序,X相等,Y就从小到大排序,那么这个问题就可以转换成类似求最大递增子序列问题一样思路的DP,DP[i]表示第i个格子时的最大值,dp[i+1]就是从前i个中找符合条件的最大的一个加上去。 1 #include 2 #include 3 #include 4 #include 5 #include ... 阅读全文
posted @ 2013-11-09 14:56 GLSilence 阅读(160) 评论(0) 推荐(0)
摘要:Largest Rectangle in a Histogram大意:找到面积最大的矩形思路:对于每一个a[i],用dp找出a[i]左边和右边连续大于自己的数的长度 l[i]表示比a[i]大的数连续的最左边的位置 r[i]表示比a[i]大的数连续的最右边的位置 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #define LL __int6410 using namespace std;11 12 LL a[100010], dp_l[... 阅读全文
posted @ 2013-11-08 19:45 GLSilence 阅读(215) 评论(0) 推荐(0)
摘要:Bone Collector最基本的01背包 两种实现都要掌握的很熟练!二维数组实现01背包: 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #define LL long long10 using namespace std;11 12 int dp[1010][1010];13 14 void run()15 {16 int p, n, m;17 int a[1010], b[1010];18 scanf("%d",... 阅读全文
posted @ 2013-11-06 20:03 GLSilence 阅读(209) 评论(0) 推荐(0)
摘要:饭卡思路:先把最贵的一个拿出来,剩下的用背包求出最优解,用5元去买最贵的。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #define LL long long10 using namespace std;11 12 void run()13 {14 int n, m;15 int a[1010], dp[1010];16 while(~scanf("%d", &n) && n)17 {18 in... 阅读全文
posted @ 2013-11-06 16:56 GLSilence 阅读(203) 评论(0) 推荐(0)
摘要:The Twin TowersLCS水题,就是题读的时候有点费劲。 我英语是渣渣- -! 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #define LL long long10 using namespace std;11 12 void run()13 {14 int n, m;15 int dp[110][110];16 int a[110], b[110];17 int cnt = 0;18 w... 阅读全文
posted @ 2013-11-06 09:40 GLSilence 阅读(174) 评论(0) 推荐(0)
摘要:Make Palindrome大意:求一个字符串在通过在任意位置增加一个字符使得变为一个回文串的最小操作数及打印出该回文串。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namespace std;10 11 int dp[1010][1010];12 13 int Max(int x, int y)14 {15 if(x >= y)16 return x;17 return y;18 }19 20 ... 阅读全文
posted @ 2013-10-29 19:18 GLSilence 阅读(214) 评论(0) 推荐(0)
摘要:Bad Hair Day开个标记数组记录,记录右边第一个比它大的位置。#include #include #define N 80001long long p[N],left[N],sum;int main(){ int n,i,j; scanf("%d",&n); for(i = 1; i = 1; i--) { j = i+1; while(j #include using namespace std;const int N = 80005;int stack[N];int top, n;int main(){ int num; ... 阅读全文
posted @ 2013-10-24 21:35 GLSilence 阅读(255) 评论(0) 推荐(0)
摘要:最长公共子序列其实有很多方法可以过,因为在弄DP所以就只写了动态规划的方法:动态规划方法1、序列str1和序列str2 ·长度分别为m和n; ·创建1个二维数组L[m.n]; ·初始化L数组内容为0 ·m和n分别从0开始,m++,n++循环: - 如果str1[m] == str2[n],则L[m,n] = L[m - 1, n -1] + 1; - 如果str1[m] != str2[n],则L[m,n] = max{L[m,n - 1],L[m - 1, n]} ·最后从L[m,n]中的数字一定是最大的,且这个数字就是最长公共子序列的长度 阅读全文
posted @ 2013-08-21 16:27 GLSilence 阅读(459) 评论(0) 推荐(0)