随笔分类 -  dp

FZU 2079 最大获利
摘要:线段树优化DP是dp[i]=max{dp[j]+w[j+1][i],dp[i-1]},其中1=<i<=n,0=<j<=j-1;dp[i]表示前i个项目的最大获利,w[j][i]表示项目j到i全部投资的获利。#include<stdio.h> #include<string.h> #include<stdlib.h> typedef long long ll; int n,m; int a[20010]; struct Item{ int l,r; ll v; }item[20010]; ll dp[20010],w[70010],p[ 阅读全文

posted @ 2012-04-29 13:22 c语言源码 阅读(213) 评论(0) 推荐(0)

POJ 1947
摘要:给定一棵N个节点的无根树,求至少要删去多少条边,使得分离后的若干个独立的子树之中的某一子树恰好有P个节点。TreeDP。树形动态规划。设状态f[i, j]表示以节点i为根的子树恰好有j个节点至少需要删除的边的数目。对于每个节点u。我们可以对它的儿子们(*儿子的状态必然已更新)进行一次类似背包的DP。(泛化物品的背包)r[i, j]表示由在前i个儿子中构成j个节点(*这里的j个节点不包括根节点)的以节点u为根的子树最少需要删除的边的数目。于是就有:r[i, j] = min{r[i - 1, j] + 1, r[i - 1, k] + f[g[u, i], j – k]},k /in [0..j 阅读全文

posted @ 2012-04-08 12:22 c语言源码 阅读(119) 评论(0) 推荐(0)

HDU 4001 To Miss Our Children Time
摘要:一开始我一直想的是简单图论问题,构图SPFA求最长路,发现这样做有环不好处理。比如这组样例:21 1 1 01 1 1 0构图就会有环,不能直接SPFA,tarjan缩点的话应该可以做,但是代码量就太大了。可以这样去处理:bool cmp(block a,block b) { if(a.x!=b.x)return a.x<b.x; if(a.y!=b.y)return a.y<b.y; return a.d>b.d; } void build(){ //缩点,避免产生环 sort(x,x+n,cmp); int cnt=1; y[1]=x[0]; for(int i=1;i& 阅读全文

posted @ 2012-03-24 21:38 c语言源码 阅读(201) 评论(0) 推荐(0)

BOJ 396
摘要:Problem K. Alice's PianoDescriptionAlice has a piano which can plays nice music, but it's different from otherpianos. It has two rows of keys, and the method of playing it is also quitespecial. When Alice plays piano, she has to put her two hands on the leftsideof the two rows of keys respec 阅读全文

posted @ 2012-03-24 19:39 c语言源码 阅读(151) 评论(0) 推荐(0)

BOJ 394
摘要:Problem I. The Longest Sequence ofRectanglesDescriptionA rectangle is specified by a pair of coordinates (x1 , y1) and (x2 , y2)indicating its lower-left and upper-right corners (x1 <= x2 and y1 <= y2). Fora pair of rectangles, A = ((X_A1, Y_A1), (X_A2, Y_A2)) and B = ((X_B1, Y_B1),(X_B2, Y_B2 阅读全文

posted @ 2012-03-24 19:10 c语言源码 阅读(177) 评论(0) 推荐(0)

导航