随笔分类 -  dp

摘要:一个简单的dp,比赛的时候太坚信自己的小聪明没用二维数组一直WA到死;#include#include#define maxn 1009#define mod 1000000007using namespace std;int num[maxn];long long ci[maxn][1026],a... 阅读全文
posted @ 2014-07-31 22:06 Yours1103 阅读(234) 评论(0) 推荐(0)
摘要:C题,#include#include#include#define maxn 5005using namespace std;int num[maxn];int rmq(int l,int r){ int ans=1num[i]) { ans=num[i]... 阅读全文
posted @ 2014-07-18 10:15 Yours1103 阅读(200) 评论(0) 推荐(0)
摘要:约瑟夫题目变形,思路和前面白书那个例题差不多~~#include#include#include#include#define maxn 500009using namespace std;int dp1[maxn];int dp2[maxn];int dp3[maxn];int main(){ ... 阅读全文
posted @ 2014-07-16 14:16 Yours1103 阅读(123) 评论(0) 推荐(0)
摘要:状态压缩dp#include#include#include#include#include#define maxn 16using namespace std;long long dp[1<<maxn];long long weapon[1<<maxn];int robot[maxn];char ... 阅读全文
posted @ 2014-07-15 17:00 Yours1103 阅读(137) 评论(0) 推荐(0)
摘要:dp[i][j]意思是前i个分成j组最小的花费#include#include#include#include#include#define maxn 110using namespace std;int dp[maxn][maxn];int vis[maxn][maxn];int num[maxn... 阅读全文
posted @ 2014-07-15 15:13 Yours1103 阅读(125) 评论(0) 推荐(0)
摘要:View Code 阅读全文
posted @ 2014-07-15 13:59 Yours1103 阅读(130) 评论(0) 推荐(0)
摘要:状态压缩dp#include#include#include#define maxn 4100using namespace std;int map[13][13];int dp[maxn][510];int gcd(int x,int y){ return y==0?x:gcd(y,x%y)... 阅读全文
posted @ 2014-04-22 16:59 Yours1103 阅读(220) 评论(0) 推荐(0)
摘要:有依赖的背包,转化成01背包来做;#include#include#include#includeusing namespace std;int n,m;int f[100009];int dp[100009];int main(){ int num,value,pen,pp; while(scanf("%d%d",&n,&m)!=EOF) { memset(f,0,sizeof f); for(int i=1; i=pen; j--) { dp[j]=max(dp[j],dp[j... 阅读全文
posted @ 2014-04-08 15:37 Yours1103 阅读(142) 评论(0) 推荐(0)
摘要:有依赖的背包,用树形dp解#include#include#include#include#define maxn 205using namespace std;int n,m;int f[maxn][maxn];struct node{ int pre; int cnt_son; int son[maxn]; int value;} no[maxn];void dfs(int x){ f[x][1]=no[x].value; int v; for(int i=0; i1; j--)//从大往小更新,结果不会覆盖; { ... 阅读全文
posted @ 2014-04-08 14:13 Yours1103 阅读(123) 评论(0) 推荐(0)
摘要:背包,输出方案数!#include#include#include#define maxn 505using namespace std;int f[maxn];int cot[maxn];int num[maxn];int main(){ int n,m,t; scanf("%d",&t); while(t--) { int mi=1=num[i]; j--) { if(f[j]==f[j-num[i]]+1) cot[j]+=cot[j-num[i]]; ... 阅读全文
posted @ 2014-04-07 14:23 Yours1103 阅读(165) 评论(0) 推荐(0)
摘要:01背包,输出方案#include#include#include#define maxn 100009using namespace std;int f[maxn];int num[30];int g[30][maxn];int cot[30];int main(){ int n,v; int sum; while(scanf("%d",&sum)!=EOF) { scanf("%d",&n); memset(f,0,sizeof f); memset(g,0,sizeof g); for(int i=1;i=0... 阅读全文
posted @ 2014-04-03 21:41 Yours1103 阅读(172) 评论(0) 推荐(0)
摘要:简单dp#include#define maxn 10005#include#includeusing namespace std;double dp[140][maxn];double t[140][140];int num[maxn];int main(){ int n,m; while(scanf("%d",&m)!=EOF) { m=(m*(m-1)*(m-2)/6); for(int i=0; ima2)ma2=dp[i][j]; } ma1=ma2; } ma1=0... 阅读全文
posted @ 2014-03-13 16:28 Yours1103 阅读(183) 评论(0) 推荐(0)
摘要:斜率dp#include#include#include#include#define maxn 10005#define maxm 5005using namespace std;int dp[maxn][maxm];int q[maxn*10];int num[maxn];bool check(int j,int tail,int i){ int yk=dp[q[tail-1]][j-1]+num[q[tail-1]+1]*num[q[tail-1]+1]; int yj=dp[q[tail]][j-1]+num[q[tail]+1]*num[q[tail]+1]; in... 阅读全文
posted @ 2014-03-12 17:13 Yours1103 阅读(120) 评论(0) 推荐(0)
摘要:Good Serial Inc.比较简单;#include#include#include#define maxn 1000009#define ll long longusing namespace std;const ll mod=987654321;ll f[maxn]; int main(){ ll n,m; f[1]=1; for(ll i=2;imod)f[i]%=mod; } while(scanf("%lld%lld",&n,&m)&&(n+m)>0) { ll ans=1; if(n0) {... 阅读全文
posted @ 2014-03-11 17:33 Yours1103 阅读(175) 评论(0) 推荐(0)
摘要:lcis#include#include#include#define maxn 504using namespace std;int a[maxn];int b[maxn];int f[maxn];int pre[maxn][maxn];int biao[maxn];int main(){ int n,m; int t; while(scanf("%d",&n)!=EOF) { memset(f,0,sizeof f); memset(pre,0,sizeof pre); for(int i=1; ib[j]&&ma0;i--... 阅读全文
posted @ 2014-03-08 17:09 Yours1103 阅读(136) 评论(0) 推荐(0)
摘要:这个题目看似不是很好下手,不过很容易发现每次询问的时候总是会问到第r个盒子是否有糖果;这样的话就很好办事了;维护两个数组;一个sum数组:累加和;一个in数组:如果i位是1的话,in[i]=in[i-k]+1;否则不加1,很好理解;然后利用in数组可以找到本来应该有糖果的但是没有糖果的箱子的数目;然后结合sum数组就可以的出结果;#include#include#define maxn 100005using namespace std;char s[maxn];int sum[maxn];int in[maxn];int n,w,k;int main(){ scanf("%d%d% 阅读全文
posted @ 2014-02-23 11:00 Yours1103 阅读(348) 评论(0) 推荐(0)
摘要:概率dp,有点像背包的做法;dp[i][j]代表前i个数组成的j数的概率为多少#include#include#define maxn 40009using namespace std;double dp[45][maxn];int s;double sco;int main(){ int t,n; scanf("%d",&t); while(t--) { memset(dp,0,sizeof dp); dp[0][0]=1; scanf("%d%lf",&n,&sco); for(int i=0;i=sco) ... 阅读全文
posted @ 2014-02-19 08:27 Yours1103 阅读(186) 评论(0) 推荐(0)
摘要:这个题是计算不同子序列的和;spoj上的那个同名的题是计算不同子序列的个数;其实都差不多;计算不同子序列的个数使用dp的思想;从头往后扫一遍如果当前的元素在以前没有出现过,那么dp[i]=dp[i-1]*2+1;不然找到最右边的这个元素出现的位置j;dp[i]=d[i]*2-dp[j];spoj代码:#include#include#include#define mod 1000000007using namespace std;char s[100005];int pos[100005];int biao[30];int dp[100005];int main(){ int t,n; ... 阅读全文
posted @ 2013-12-11 22:47 Yours1103 阅读(169) 评论(0) 推荐(0)
摘要:这个题我想到要用kmp找到循环节;但是后面的我就不会做了;看到题解才知道是字符串的最小表示;#include#include#include#define maxn 100005using namespace std; char s[maxn*2];int next[maxn]; void kmp(int n){ int j=0; for(int i=2;i0&&s[i]!=s[j+1])j=next[j]; if(s[i]==s[j+1])++j; next[i]=j; }} void MinimumRepresentation(int n){ ... 阅读全文
posted @ 2013-12-11 22:41 Yours1103 阅读(158) 评论(0) 推荐(0)
摘要:这是一个动态规划的题;当初想到要用dp,但是一直想不到状态转移的方程;题解上的原话: 动态规划,设 g[i]表示总结点数为 i 的方案种数,另设 f[i][j]表示各个孩子的总结点数为i,孩子的个数为 j 的方案数,那么有 g[i+1]=f[i][1]+f[i][2]+...+f[i][k],相当于添加一个根节点之后变成完整的树,同时要把有 1 个孩子,2个孩子, ……,k 个孩子的情况都考虑进去。对于 f[i][j]的求解可以用类似背包的方法去做,在求解的时候也会用到 g[1], g[2], ..., g[i]的值,根据前面的那个 g[i+1]的表达式来看,这些 g[]已经在前面算出来了。. 阅读全文
posted @ 2013-12-11 22:32 Yours1103 阅读(140) 评论(0) 推荐(0)