Apple Catching POJ - 2385(基础的动态规划算法)

题意:

给你两个数字n和m;代表会有n个苹果掉落,m次可以移动的机会;有两棵树,开始你站在树1下面,一分钟只能移动一次,下面的数值代表在哪一颗树下会掉落苹果;问你在可移动的范围内,最多可以接到多少个苹果?

题目:

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.

分析:

(1).基础dp题,按照题意很容易看出有两个变量,分钟和移动机会,所以我们按照习惯写出两层for循环,简单思考其中的关系,就可以得到dp定义,dp[i][j]dp[i][j]dp[i][j],表示在i时间内,用 jjj次转移机会得到的最大苹果数.
(2).因为开始的位置在第一棵树下,那么我们就可以由移动的步数 jjj的奇偶性判断现在在哪颗树下;
(3).dp转移如下,如果 j==0j==0j==0,则 dp[i][j]=dp[i−1][j]dp[i][j]=dp[i-1][j]dp[i][j]=dp[i1][j],考虑j为0时,到第i分钟,一步都没有走动。
否则 dp[i][j]=max(dp[i−1][j],dp[i−1][j−1])dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])dp[i][j]=max(dp[i1][j],dp[i1][j1]).就是当前从上一个状态下,走还是不走获得苹果最多。
(4)如果现在所在的树的编号和现在掉落的苹果所在的位置相同,那么接到的苹果的数量姐增加,由(2)这个我就放在最后判断一下就行,也可以放在第三步,直接在推导式里面。
(5).最后在 dp[n][i]dp[n][i]dp[n][i]里找最大值就行了,(0<=i<=n).(0<=i<=n).(0<=i<=n).表示一共走i步时,n分钟吃到苹果最大值。
我今天真的闲的罗里吧嗦这么多,自恋的觉得只要跟着我的思路走,一定能会这道题,hhh

AC模板:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e3+10;
int n,m,ans;
int s[M],dp[M][35];
int main()
{while(~scanf("%d%d",&n,&m)){ans=0;memset(dp,0,sizeof(dp));for(int i=1; i<=n; i++)scanf("%d",&s[i]);if(s[1]==1)dp[1][0]=1;elsedp[1][1]=1;for(int i=2; i<=n; i++)for(int j=0; j<=i&&j<=m; j++){if(j==0)//考虑j为0时,也就是说,到第i分钟,一步都没有走动。dp[i][j]=dp[i-1][j]+s[i]%2;else{dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);//就是当前走还是不走获得苹果最多。if(j%2+1==s[i])//因为最开始站在树1下面,可以用走多少步的奇偶来表示当前在那棵树下。dp[i][j]++;}}for(int i=0; i<=m; i++)//表示一共走i步时,n分钟吃到苹果最大值。ans=max(ans,dp[n][i]);printf("%d\n",ans);}return 0;
}

备战ccpc分站赛ing ,题目分析简略,见谅,转载请注明出处。。。。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309577.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于 abp vNext 和 .NET Core 开发博客项目 - 用AutoMapper搞定对象映射

上一篇文章集成了定时任务处理框架Hangfire&#xff0c;完成了一个简单的定时任务处理解决方案。本篇紧接着来玩一下AutoMapper&#xff0c;AutoMapper可以很方便的搞定我们对象到对象之间的映射关系处理&#xff0c;同时abp也帮我们是现实了IObjectMapper接口&#xff0c;先根…

磁盘文件系统、挂载

参考&#xff1a;https://zhuanlan.zhihu.com/p/106459445 https://blog.csdn.net/qq_39521554/article/details/79501714 文件系统 持久化的数据是存储在外部磁盘上的&#xff0c;如果没有文件系统&#xff0c;访问这些数据需要直接读写磁盘的sector&#xff0c;而文件系统存…

[Java基础]Stream流的收集操作

代码如下: package CollectPack;import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream;public class CollectDemo {public static void main(String[] args){List<String> list new ArrayList<String>();list.add("林青…

Bound Found POJ - 2566(尺取法+前缀和创造区间变化趋势)

题意&#xff1a; 给定一个数组和一个值t&#xff0c;求一个子区间使得其和的绝对值与t的差值最小&#xff0c;如果存在多个&#xff0c;任意解都可行。 题目&#xff1a; Signals of most probably extra-terrestrial origin have been received and digitalized by The Ae…

MESI 缓存一致性协议

MESI 缓存一致性协议 多核场景下&#xff0c;仅仅依靠CAS这一类硬件原语并不能实现同步&#xff0c;因为原子指令底层实现也可能包含多条微指令&#xff0c;而原子指令的原子性是相对于一个核而言的&#xff0c;多条原子指令在各自的CPU核上都是原子执行的。所以要解决这个问题…

15分钟从零开始搭建支持10w+用户的生产环境(一)

前言这是一个基于中小型企业或团队的架构设计。不考虑大厂。有充分的理由相信&#xff0c;大厂有绝对的实力来搭建一个相当复杂的环境。中小型企业或团队是个什么样子&#xff1f;开发团队人员配置不全&#xff0c;部分人员身兼开发过程上下游的数个职责&#xff1b;没有专职的…

Sum of Consecutive Prime Numbers POJ - 2739(线性欧拉筛+尺取法)

题意&#xff1a; 一些正整数可以由一个或多个连续质数的总和表示。给定一个的正整数n,问满足条件的有多少种情况&#xff1f; 题目&#xff1a; Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representat…

高性能IO——Reactor模式

高性能IO——Reactor模式 参考&#xff1a;https://cloud.tencent.com/developer/article/1513447 目前的IO线程处理模型一般可以分为以下三类&#xff1a; 单线程阻塞I/O服务模型&#xff1b; while(true) {socket accept();handle(socket) }多线程阻塞I/O服务模型&#xf…

X-lab 开放实验室开源创新的故事

本报告为“开源软件供应链点亮计划暑期2020活动”中的“大咖说开源”第二期的特邀嘉宾视频&#xff0c;正好借此机会给大家介绍下 X-lab 实验室目前在开源方面开展的一些事情&#xff0c;欢迎大家关注&#xff0c;也欢迎更多热爱开源的朋友们加入&#xff01;摘要&#xff1a;2…

Circle and Points POJ - 1981(单位圆覆盖最多点)

题意&#xff1a; 给你n个点和点的位置&#xff0c;问单位圆最多能覆盖多少个点。 题目&#xff1a; You are given N points in the xy-plane. You have a circle of radius one and move it on the xy-plane, so as to enclose as many of the points as possible. Find h…

Golang 匿名函数、闭包

参考&#xff1a;https://blog.csdn.net/qq_35976351/article/details/81986496 Golang 闭包 匿名函数 Golang支持匿名函数&#xff0c;即在需要使用函数时&#xff0c;再定义函数&#xff0c;匿名函数没有函数名&#xff0c;只有函数体。 匿名函数经常被用于实现回调函数、闭…

ASP.NET Core分布式项目实战(Consent 确认逻辑实现)--学习笔记

任务22&#xff1a;Consent 确认逻辑实现接下来&#xff0c;我们会在上一节的基础上添加两个按钮&#xff0c;同意和不同意&#xff0c;点击之后会把请求 post 到 ConsentController 处理&#xff0c;如果同意会通过 return url 跳转到客户端&#xff0c;如果不同意就会取消&am…

The Last Non-zero Digit POJ - 1150(n!mod p)

题意&#xff1a; 要求你求出n!(n−m)!)\frac{n!}{(n-m)!)}(n−m)!)n!​中最后一个非0的数字. 题目&#xff1a; In this problem you will be given two decimal integer numberN,M. You will have to find the last non-zero digit of the NPM^{N}P_{M}NPM​.This means n…

Istio 1.6——迈向极简主义

从 1.2 版本开始&#xff0c;Istio 进入季度发布的节奏。5 月 21 日发布的 1.6 版本可以说是最准时的一次。我们是否可以理解 Istio 架构简化后的开发工作已经步入了正轨&#xff1f;这次的更新是否会带给我们惊喜&#xff1f;亦或是还有遗憾&#xff1f;让我们一一道来。&…

[Java基础]获取Class类的对象

代码如下: package ClassObjectPack;public class Student {private String name;int age;public String address;public Student(String name, int age, String address) {this.name name;this.age age;this.address address;}public Student() {}private Student(String …

使用PInvoke互操作,让C#和C++愉快的交互优势互补

一&#xff1a;背景1. 讲故事如果你常翻看FCL的源码&#xff0c;你会发现这里面有不少方法借助了C/C的力量让C#更快更强悍,如下所示&#xff1a;[DllImport("QCall", CharSet CharSet.Unicode)][SecurityCritical][SuppressUnmanagedCodeSecurity]private static ex…

蓝桥杯2014届试题9题 小朋友排队(树状数组+类逆序对)

题目&#xff1a; 资源限制 时间限制&#xff1a;1.0s 内存限制&#xff1a;256.0MB 问题描述 n 个小朋友站成一排。现在要把他们按身高从低到高的顺序排列&#xff0c;但是每次只能交换位置相邻的两个小朋友。 每个小朋友都有一个不高兴的程度。开始的时候&#xff0c;所有…

[Java基础]反射获取构造方法并使用

代码如下: package ClassObjectPack;import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException;public class ReflectDemo01 {public static void main(String[] args) throws ClassNotFoundExcep…