Apple Catching(POJ 2385)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9978 | Accepted: 4839 |
Description
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
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Output
Sample Input
7 2
2
1
1
2
2
1
1
Sample Output
6
Hint
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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int i,j,t,n,Max=;
int dp[+][];
int a[+];
freopen("in.txt","r",stdin);
scanf("%d%d",&t,&n);
for(i=;i<=t;i++)
scanf("%d",&a[i]);
for(i=;i<=t;i++)
{
dp[i][]=dp[i-][]+-a[i]; //dp[i][j]={第i分钟走了j步所摘到的苹果}
for(j=;j<=n;j++)
{
if(j%) //如果为奇数,说明走到了2树
dp[i][j]=max(dp[i-][j],dp[i-][j-])+a[i]-; //取前一分钟的最大值
else
dp[i][j]=max(dp[i-][j],dp[i-][j-])+-a[i];
}
}
for(i=;i<=n;i++)
Max=max(dp[t][i],Max);
printf("%d\n",Max); }
Apple Catching(POJ 2385)的更多相关文章
- poj2385 Apple Catching (线性dp)
题目传送门 Apple Catching Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 154 ...
- POJ 2385 Apple Catching(01背包)
01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...
- 【POJ - 2385】Apple Catching(动态规划)
Apple Catching 直接翻译了 Descriptions 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速 ...
- 【POJ】2385 Apple Catching(dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13447 Accepted: 6549 D ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- 广大暑假训练1(poj 2488) A Knight's Journey 解题报告
题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A (A - Children of the Candy Corn) ht ...
- Games:取石子游戏(POJ 1067)
取石子游戏 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37662 Accepted: 12594 Descripti ...
- BFS 或 同余模定理(poj 1426)
题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple 非零倍数 啊. 英语弱到爆炸,理解不了题意... ...
随机推荐
- 根据样式获取被选中的checkbox
<ul id="> </div> </li></ul> $("#btn_CheckManagerCompletionCreateAt ...
- C51汇编伪指令
1.DS ---预留存储区命令格式: [标号:] DS 表达式值其功能是从指定地址开始,定义一个存储区,以备源程序使用.存储区预留的存储单元数由表达式的值决定. ;从标号TEP地址处开始保留1个存 ...
- 《Programming WPF》翻译 第5章 2.内嵌样式
原文:<Programming WPF>翻译 第5章 2.内嵌样式 每一个“可样式化”的WPF元素都有一个Style属性,可以在内部设置这个属性--使用XAML属性-元素的语法(在第一章讨 ...
- Deploy a Sharded Cluster
Start the Config Server Database Instances for example : mongod --configsvr --dbpath <path> - ...
- spring框架源码编译
程序猿小菜一枚,最近从github上面下载下来一份spring各个项目的源码一份,但是弄了很长时间,因为网速和(fanqiang)的速度,希望大家不要再为这种无谓的时间花费时间,简单来说github上 ...
- Shortest Word Distance 解答
Question Given a list of words and two words word1 and word2, return the shortest distance between t ...
- C#基于UDP实现的P2P语音聊天工具(1)
这篇文章主要是一个应用,使用udp传送语音和文本等信息.在这个系统中没有服务端和客户端,相互通讯都是直接相互联系的.能够很好的实现效果. 语音获取 要想发送语音信息,首先得获取语音,这里有几种方法,一 ...
- 平时的笔记02:处理mp3
#! /usr/bin/env python # # mutagen aims to be an all purpose media tagging library # Copyright (C) 2 ...
- C#分层开发MySchool
分层开发之MYSCHOOL No.1实现登陆功能,验证用户名和密码.从数据库里进行匹配,看是否有符合要求的数据. 在DAL层编写代码,返回值为布尔类型.方法参数为(student实体类对象),使用参数 ...
- springMVC+mybatis用户登录实例
1.整体结构 2.准备工作 数据库: --Mysql 5.6 创建数据库 wolf 1 CREATE DATABASE wolf; 创建用户表 user 1 2 3 4 5 6 create tabl ...