POJ2385--Apple Catching
来源:互联网 发布:c语言 double 误差 编辑:程序博客网 时间:2024/05/01 05:31
题目大意:Farmer John一开始是站在树1下面,他可以在树1和树2之间来回移动W次,每分钟只能移动一次,也可以不移动每分钟有且只有一棵树会掉苹果,问在时间为T分钟内,他可以得到多少个苹果
分析:状态:dp[i][j]表示在第i分钟时,已经移动了j次后得到的苹果数量。
状态转移方程:dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]),然后判断当前是否在第i分钟掉苹果的那颗树下,是的话,dp[i][j]++。
对状态转移方程的解释如下:第i分钟能得到的苹果数量,等于在第i-1分钟时,在树1和树2下得到苹果的最大值。j为偶数则在树1下面,奇数则在树2下面。
代码:
#include <cstdio>#include <algorithm>using namespace std;const int maxn = 1111;int a[maxn], dp[maxn][40];int main() { int t, w; while(~scanf("%d%d", &t, &w)) { for(int i = 1; i <= t; i++) scanf("%d", &a[i]); if(a[1] == 1) { dp[1][0] = 1; dp[1][1] = 0; } else { dp[1][0] = 0; dp[1][1] = 1; } for(int i = 2; i <= t; i++) { for(int j = 0; j <= w; j++) { if(j == 0) dp[i][j] = dp[i-1][j] + a[i]%2; else { dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]); if(j%2+1 == a[i]) dp[i][j]++; } } } int ans = 0; for(int i = 0; i <= w; i++) ans = max(ans, dp[t][i]); printf("%d\n", ans); } return 0;}
0 0
- poj2385 - Apple Catching
- POJ2385 Apple Catching
- POJ2385 Apple Catching 【DP】
- poj2385 Apple Catching
- POJ2385--Apple Catching
- POJ2385-Apple Catching
- poj2385 Apple Catching
- POJ2385-Apple Catching
- POJ2385 Apple Catching
- poj2385 Apple Catching
- POJ2385 Apple catching(dp)
- POJ2385 Apple Catching 动态规划
- 【动态规划】poj2385 Apple Catching
- POJ2385——Apple Catching
- 【POJ2385】Apple Catching(简单DP)
- POJ2385-Apple Catching-简单dp
- POJ2385 Apple Catching (dp)
- POJ2385 Apple Catching——动态规划
- 人活着系列之Streetlights
- 第一个IOS程序的开发过程以及详细步骤介绍(一)
- ImageView、Adapter
- imitate wechat - 1
- ubuntu 14.04安装opencv3.0.0
- POJ2385--Apple Catching
- 一种简单的linux命令行解析方法
- MyEclipse2014完美破解方法
- 设计思想与设计模式
- Linux:awk命令详解
- poj2031Building a Space Station 最小生成树
- 关于java jdk和jre
- Defining an Interaction Model: The Cornerstone of Application Design
- Ubuntu14.04上配置hadoop