poj2385【基础DP】
来源:互联网 发布:淘宝店铺投诉管用吗 编辑:程序博客网 时间:2024/04/30 03:58
挑战DP
题意:
有两棵树,给每分钟有苹果掉落的苹果树,一开始B在 1 树下,B最多只能移动W步,求一个最大数量拿到的。
思路:
一开始想的是,我对于每分钟情况来说无非是等于之前的在本位置+1,或者等于之前的非本位置,取个较大值。然后我想对于非本位置的就要对于步数+1,这可怎么记录呢,难死我了。困难就是,对于前i分钟他可以怎么换几步我不确定啊,额。。。然后可以确定的是前i分钟最多i步且小于等于w步吧…这可以吧,那么我对于前i分钟我就考虑对于这个点能移几步都算出了,保证了无后效性,最优性。我还可以调用前面的,完美。
对于这个位置,我移了j步,在原位置+1,非原位置+0,一共两棵树,所以和奇偶有很大关系,temp=j%2==0 ?2- a[i] : a[i]-1;
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+temp;
ok,贴一发挫code…….
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int Mod=1e9;const int INF=0x3f3f3f3f;const int N=1e3+10;int a[N];int dp[N][33];int main(){ int n; int w,j,k; scanf("%d%d",&n,&w); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } memset(dp,0,sizeof(dp)); int temp; for(int i=1;i<=n;i++) { dp[i][0]=dp[i-1][0]+2-a[i]; //0次 for(int j=1;j<=w&&j<=i;j++) //1-w次 { if(j%2) temp=a[i]-1; else temp=2-a[i]; dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+temp; } } int ans=0; for(int i=0;i<=w;i++) ans=max(dp[n][i],ans); printf("%d",ans); return 0;}
0 0
- poj2385【基础DP】
- poj2385 dp
- poj2385 dp
- poj2385(dp)
- POJ2385【DP】
- dp-poj2385
- 基础DP-poj2385-数塔DP变形-挑战程序设计2
- poj2385-dp经典
- poj2385(DP)
- poj2385-dp经典
- POJ2385 Apple Catching 【DP】
- POJ2385 简单DP
- [dp]poj2385 AppleCatching
- POJ2385 经典DP
- POJ2385 Apple catching(dp)
- 【POJ2385】Apple Catching(简单DP)
- POJ2385-Apple Catching-简单dp
- POJ2385 Apple Catching (dp)
- MYSQL实现主从复制的日志是哪种?
- 多态的实现原理——多态的理解
- HDU 5805 - NanoApe Loves Sequence
- Tomcat8如何开启APR运行模式(linux centos)
- ReActivieX学习资料汇总
- poj2385【基础DP】
- 关于linux下执行脚本编译C++:需要使用make命令和makefile
- 认识CoreData—基础使用
- ubuntu 安装配置shh,tftp,nfs 笔记
- C语言的基本单位是
- 求二叉树的深度
- Qwt源码解读之QwtIntervalSample类和QwtSetSample类
- 详解Fragment生命周期
- C++封装C#中的事件