简单暴力到dp的优化(中级篇)

兔老大 发表于 2021/04/21 01:05:19 2021/04/21
【摘要】 下面再放三道我比较喜欢的,需要好好写一下的题。 第一题比较水 1. White Cloud is exercising in the playground. White Cloud can walk 1 meters or run k meters per second. Since White Cloud is tired,it can't run for two or...

下面再放三道我比较喜欢的,需要好好写一下的题。

第一题比较水

1. White Cloud is exercising in the playground. White Cloud can walk 1 meters or run k meters per second. Since White Cloud is tired,it can't run for two or more continuous seconds. White Cloud will move L to R meters. It wants to know how many different ways there are to achieve its goal. Two ways are different if and only if they move different meters or spend different seconds or in one second, one of them walks and the other runs.

输入描述: The first line of input contains 2 integers Q and k.Q is the number of queries.(Q<=100000,1<=k<=100000) For the next Q lines,each line contains two integers L and R.(1<=L<=R<=100000)

输出描述: For each query,print a line which contains an integer,denoting the answer of the query modulo 1000000007.

示例1: 输入 3 3 3 3 1 4 1 5 输出 2 7 11

题目大意

白云在健身,每秒可以走1米或跑k米,并且不能连续两秒都在跑。 当它的移动距离在[L,R]之间时,可以选择结束锻炼。 问有多少种方案结束。

做法

DP? f[i][0/1]表示已经过了i米,最后一步是跑还是走的方案数,分开就很好写了,简单,自己体会。 f[i][1]=f[i-k][0],f[i][0]=f[i-1][0]+f[i-1][1] 注意要记录前缀和。

 


  
  1. #include <bits/stdc++.h>
  2. #define MOD 1000000007
  3. using namespace std;
  4. typedef long long ll;
  5. ll g[100005];
  6. ll dp[100005][2];
  7. int main(void)
  8. {
  9. int q,k,a,b;
  10. ll sum;
  11. cin>>q>>k;
  12. dp[0][0]=1;
  13. for(int i=1;i<=100000;++i){
  14. dp[i][0]=(dp[i-1][0]+dp[i-1][1])%MOD;
  15. if(i-k>=0)dp[i][1]=(dp[i-k][0])%MOD;
  16. }
  17. sum=0;
  18. for(int i=1;i<=100000;++i){
  19. g[i]=(dp[i][0]+dp[i][1]+sum);
  20. sum=g[i];
  21. }
  22. // for(int i=9000;i<=100000;++i){
  23. // cout<<g[i]<<endl;
  24. // }
  25. while(q--){
  26. cin>>a>>b;
  27. cout<<(g[b]-g[a-1])%MOD<<endl;
  28. }
  29. return 0;
  30. }

 

2、来一发多重背包

选这个题是感觉代码有一种美感。。。。。

一. 编程题

1. Since then on, Eddy found that physics is actually the most important thing in the contest. Thus, he wants to form a team to guide the following contestants to conquer the PACM contests(PACM is short for Physics, Algorithm, Coding, Math).

There are N candidate groups each composed of pi physics experts, ai algorithm experts, ci coding experts, mi math experts. For each group, Eddy can either invite all of them or none of them. If i-th team is invited, they will bring gi knowledge points which is calculated by Eddy's magic formula. Eddy believes that the higher the total knowledge points is, the better a team could place in a contest. But, Eddy doesn't want too many experts in the same area in the invited groups. Thus, the number of invited physics experts should not exceed P, and A for algorithm experts, C for coding experts, M for math experts.

Eddy is still busy in studying Physics. You come to help him to figure out which groups should be invited such that they doesn't exceed the constraint and will bring the most knowledge points in total.

输入描述: The first line contains a positive integer N indicating the number of candidate groups. Each of following N lines contains five space-separated integer p i, ai, ci, mi, gi indicating that i-th team consists of pi physics experts, ai algorithm experts, ci coding experts, mi math experts, and will bring gi knowledge points. The last line contains four space-separated integer P, A, C, M indicating the maximum possible number of physics experts, algorithm experts, coding experts, and math experts, respectively.

 1 ≤ N ≤ 36 0 ≤ pi,ai,ci,mi,gi ≤ 36 0 ≤ P, A, C, M ≤ 36

输出描述: The first line should contain a non-negative integer K indicating the number of invited groups. The second line should contain K space-separated integer indicating the index of invited groups(groups are indexed from 0).

You can output index in any order as long as each index appears at most once. If there are multiple way to reach the most total knowledge points, you can output any one of them. If none of the groups will be invited, you could either output one line or output a blank line in the second line.

题干有点长,其实就是每个队有四种代价,一个价值。多重背包。36的五次方竟然没超时也是醉了。。

 


  
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=37;
  4. int n, p[N], a[N], c[N], m[N], g[N];
  5. int P, A, C, M;
  6. short dp[N][N][N][N][N];
  7. bool tk[N][N][N][N][N];
  8. int main(){
  9. cin>>n;
  10. for(int i=0; i<n; i++)
  11. cin>>p[i]>>a[i]>>c[i]>>m[i]>>g[i];
  12. cin>>P>>A>>C>>M;
  13. for(int i=0; i<=n; i++)
  14. for(int ip=0; ip<=P; ip++)
  15. for(int ia=0; ia<=A; ia++)
  16. for(int ic=0; ic<=C; ic++)
  17. for(int im=0; im<=M; im++)
  18. tk[i][ip][ia][ic][im]=false;
  19. for(int i=0; i<n; i++){
  20. for(int ip=P; ip>=0; ip--)
  21. for(int ia=A; ia>=0; ia--)
  22. for(int ic=C; ic>=0; ic--)
  23. for(int im=M; im>=0; im--)
  24. dp[i+1][ip][ia][ic][im]=dp[i][ip][ia][ic][im];
  25. for(int ip=P; ip>=p[i]; ip--)
  26. for(int ia=A; ia>=a[i]; ia--)
  27. for(int ic=C; ic>=c[i]; ic--)
  28. for(int im=M; im>=m[i]; im--)
  29. if(dp[i][ip-p[i]][ia-a[i]][ic-c[i]][im-m[i]]+g[i]>
  30. dp[i+1][ip][ia][ic][im]){
  31. dp[i+1][ip][ia][ic][im]=dp[i][ip-p[i]][ia-a[i]][ic-c[i]][im-m[i]]+g[i];
  32. tk[i+1][ip][ia][ic][im]=true;
  33. }
  34. }
  35. fprintf(stderr, "ans=%d\n", dp[n][P][A][C][M]);
  36. vector<int> ans;
  37. for(int i=n; i>=1; i--)
  38. if(tk[i][P][A][C][M]){
  39. ans.push_back(i-1);
  40. P-=p[i-1];
  41. A-=a[i-1];
  42. C-=c[i-1];
  43. M-=m[i-1];
  44. }
  45. reverse(ans.begin(), ans.end());
  46. printf("%d\n", (int)ans.size());
  47. for(size_t i=0; i<ans.size(); i++)
  48. printf("%d%c", ans[i], " \n"[i+1==ans.size()]);
  49. }

3、P1040 加分二叉树

题目

https://www.luogu.org/problemnew/show/P1040

设一个 nn 个节点的二叉树tree的中序遍历为( 1,2,3,…,n1,2,3,…,n ),其中数字 1,2,3,…,n1,2,3,…,n 为节点编号。每个节点都有一个分数(均为正整数),记第 ii 个节点的分数为 di,treedi,tree 及它的每个子树都有一个加分,任一棵子树 subtreesubtree (也包含 treetree 本身)的加分计算方法如下:

subtreesubtree 的左子树的加分× subtreesubtree 的右子树的加分+ subtreesubtree 的根的分数。

若某个子树为空,规定其加分为 11 ,叶子的加分就是叶节点本身的分数。不考虑它的空子树。

试求一棵符合中序遍历为( 1,2,3,…,n1,2,3,…,n )且加分最高的二叉树 treetree 。要求输出;

(1) treetree 的最高加分

(2) treetree 的前序遍历

思路:这个题可以用动态规划或者记忆化搜索来做。因为如果要求加分最大的话,必须要求它的儿子结点加分最大,所以就有了最优子阶段。我们可以枚举根来更新最大值。

root[i,j]表示[i,j]这段序列的根,递归输出先序遍历。注意初始化,f[i][i]=v[i],当序列只有I一个元素时,f[i][i]等于这个点本身的权值,当l==r-1时,此时是空树设为1。

 


  
  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. int n,v[39],f[47][47],i,j,k,root[49][49];
  5. void print(int l,int r){
  6. if(l>r)return;
  7. if(l==r){printf("%d ",l);return;}
  8. printf("%d ",root[l][r]);
  9. print(l,root[l][r]-1);
  10. print(root[l][r]+1,r);
  11. }
  12. int main() {
  13. scanf("%d",&n);
  14. for( i=1; i<=n; i++) scanf("%d",&v[i]);
  15. for(i=1; i<=n; i++) {f[i][i]=v[i];f[i][i-1]=1;}
  16. for(i=n; i>=1; i--)
  17. for(j=i+1; j<=n; j++)
  18. for(k=i; k<=j; k++) {
  19. if(f[i][j]<(f[i][k-1]*f[k+1][j]+f[k][k])) {
  20. f[i][j]=f[i][k-1]*f[k+1][j]+f[k][k];
  21. root[i][j]=k;
  22. }
  23. }
  24. printf("%d\n",f[1][n]);
  25. print(1,n);
  26. return 0;
  27. }

 

 

 

 

 

 

 

 

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/81290789

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。