题目传送门

Apple Catching

Apple Catching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15444   Accepted: 7582

Description

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.

Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).

Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.

Input

* Line 1: Two space separated integers: T and W

* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.

Output

* Line 1: The maximum number of apples Bessie can catch without walking more than W times.

Sample Input

7 2
2
1
1
2
2
1
1

Sample Output

6

Hint

INPUT DETAILS:

Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.

OUTPUT DETAILS:

Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.

Source

题意:有两棵树,给你t分钟,每分钟有一棵树会掉落一颗苹果,再给你w个体力,每次从一棵树移动到另一棵树需要消耗1个体力,一开始你再第一棵树那里,问你t分钟后你最多能得到几个苹果?

题解:定义dp[i][j]:前i分钟后移动j步得到的最多的苹果数

那么这到第i分钟的状态就取决于前i-1分钟移动或者不移动得到的苹果树

即dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);//注意边界

还有怎么判断移动j步到哪棵树了呢,很简单,

就是偶数步回到起点,奇数步到第二棵树

代码:

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
#define MAX 1005
int dp[MAX][];
int a[MAX];
int main()
{
int n,w;
while(~scanf("%d %d",&n,&w)){
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<=w;j++)
{
if(j>=) dp[i][j]=max(dp[i-][j],dp[i-][j-]);
else dp[i][j]=dp[i-][j];
if(j%+==a[i]) dp[i][j]++;
}
}
int ans=;
for(int i=;i<=w;i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans);
} return ;
}

Apple Catching

poj2385 Apple Catching (线性dp)的更多相关文章

  1. poj2385 Apple Catching(dp状态转移方程推导)

    https://vjudge.net/problem/POJ-2385 猛刷简单dp的第一天的第一题. 状态:dp[i][j]表示第i秒移动j次所得的最大苹果数.关键要想到移动j次,根据j的奇偶判断人 ...

  2. poj 2385 Apple Catching 基础dp

    Apple Catching   Description It is a little known fact that cows love apples. Farmer John has two ap ...

  3. 【POJ】2385 Apple Catching(dp)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13447   Accepted: 6549 D ...

  4. POJ2385——Apple Catching

                                                $Apple~Catching$ Time Limit: 1000MS   Memory Limit: 6553 ...

  5. POJ 2385 Apple Catching【DP】

    题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...

  6. poj2385 - Apple Catching【动态规划】

    Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...

  7. poj2385 Apple Catching

    思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...

  8. POJ 2385 Apple Catching ( 经典DP )

    题意 : 有两颗苹果树,在 1~T 的时间内会有两颗中的其中一颗落下一颗苹果,一头奶牛想要获取最多的苹果,但是它能够在树间转移的次数为 W 且奶牛一开始是在第一颗树下,请编程算出最多的奶牛获得的苹果数 ...

  9. POJ - 2385 Apple Catching (dp)

    题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...

随机推荐

  1. noscript

    <noscript> <article id="noscript" class="error info_panel"> <head ...

  2. 脚本_统计 Linux 进程相关数量信息

    #!bin/bash#作者:liusingbon#功能:统计 Linux 进程相关数量信息,running(运行的进程),sleeping(睡眠中的进程),stoped(停止的进程),zombie(僵 ...

  3. j函数 判断以 什么开头

    1.str.charAt(index) 返回字符串中指定位置的字符. str 是字符串  我们要将获得的数据 转化为字符串 var code = res.statusCode.toString(); ...

  4. Flutter-網絡請求

    Flutter 请求网络的三种方式 flutter 请求网络的方式有三种,分别是 Dart 原生的网络请求 HttpClient.第三方网络请求 http以及 Flutter 中的 Dio.我们可以比 ...

  5. iOS----收集的一些小技巧

    1,如何将试图置于最顶层 第一种方法 _myview.layer.zPosition =5; 第二种方法 [self.view insertSubview:_MyView atIndex:999]; ...

  6. 使用Windows service创建一个简单的定时器

    一.需求 我们有时候可能会想要做一些定时任务,例如每隔一段时间去访问某个网站,或者下载一些东西到我们服务器上等等之类的事情,这时候windows service 是一个不错的选择. 二.实现 1.打开 ...

  7. Dubbo 在跨语言和协议穿透性方向的探索:支持 HTTP/2 gRPC

    Dubbo 在跨语言和协议穿透性方向上的探索:支持 HTTP/2 gRPC 和 Protobuf 本文整理自刘军在 Dubbo 成都 meetup 上分享的<Dubbo 在多语言和协议穿透性方向 ...

  8. SQL Server数据库的软硬件性能瓶颈

    在过去十年里,很多复杂的企业应用都是用Microsoft SQL Server进行开发和部署的.如今,SQL Server已经成为现代业务应用的基石,并且它还是很多大公司业务流程的核心.SQL Ser ...

  9. android开发里跳过的坑——GridView使用Glide加载图片不显示

    用grideview显示本地图片列表,用了Glide加载框架,具体调用如下: Glide.with(mContext).load(Uri.fromFile(file)).into(imageView) ...

  10. IP地址的分类及各类IP的最大网络数、网络号范围和最大主机数

    总结自谢希仁老师的<计算机网络>第五版 每一类网络地址都由两部分组成:网络号net-id+主机号host-id.IP地址的分类可以参看下图: 可以看到各个类别的区别,同时,所有的类别都是3 ...