题目链接:http://poj.org/problem?id=2385

Apple Catching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9372   Accepted: 4565

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007; int t,w,x;
int f[1005][35],a[1005]; int main ()
{
while (scanf ("%d%d",&t,&w)==2)
{
memset(f, 0,sizeof(f));
for (int i=1; i<=t; i++)
{
scanf ("%d",&a[i]);
f[i][0] = f[i-1][0]+(a[i]==1?1:0);
}
for (int j=1; j<=w; j++)
{
for (int i=1; i<=t; i++)
{
f[i][j] = max(f[i-1][j], f[i-1][j-1])+(a[i]==(j%2+1)?1:0);
}
}
printf ("%d\n",f[t][w]);
}
return 0;
}

poj2385(dp)的更多相关文章

  1. poj2385 dp(递推)

    题目链接 :http://bak3.vjudge.net/contest/136499#problem/D 题意: //转移方程dp[i][j]=max(dp[i-1][j],dp[i-1][j-1] ...

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

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

  3. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  4. DP,得到最多苹果,(POJ2385)

    题目链接:http://poj.org/problem?id=2385 题意: 牛在两棵苹果树下收集苹果,牛只能在这两棵树之间走动w次,在t时刻,某棵树会掉下苹果. 解题报告: ///dp[t][w] ...

  5. poj2385【基础DP】

    挑战DP 题意: 有两棵树,给每分钟有苹果掉落的苹果树,一开始B在 1 树下,B最多只能移动W步,求一个最大数量拿到的. 思路: 一开始想的是,我对于每分钟情况来说无非是等于之前的在本位置+1,或者等 ...

  6. poj2385(基础DP)

    题目链接:http://poj.org/problem?id=2385 题意:最开始Bessie站在树1下面,每一个单位时间有颗苹果从树1或者树2上落下来.每个单位时间Bessie可以移动一次位置,时 ...

  7. poj2385 Apple Catching (线性dp)

    题目传送门 Apple Catching Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 154 ...

  8. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  9. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

随机推荐

  1. 2015.05.12,外语,读书笔记-《Word Power Made Easy》 15 “如何谈论不同人的特点” SESSION 45

    TEASER PREVIEW 以-ous结尾的,描绘某人特点的词语包括: fawning(['fɔ:niŋ] adj.奉承的),servilely(['sә:vail] adj. 卑屈的, 奴隶的) ...

  2. 一个JavaWeb项目中使用的部分技术

    -- 2015年8月8日 1. Web框架: Spring+ SpringMVC + MyBatis Spring: 作为容器.工厂,用于解耦以及管理对象生命周期. 整合各类框架和依赖. MVC  : ...

  3. Java做一个时间的程序,为什么要除以1000*60*60*24啊。这个数字是什么意思啊。

    1000耗秒(1秒),60秒(1分),60分(1小时),24小时(1天)

  4. centos + nodejs + egg2.x 开发微信分享功能

    本文章发到掘金上,请移步阅读: https://juejin.im/post/5cf10b02e51d45778f076ccd

  5. 12. Integer to Roman[M]整数转罗马数字

    题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  6. 17. Letter Combinations of a Phone Number[M]电话号码的字母组合

    题目 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that ...

  7. Underscore模板的使用

    一.开篇 下载underscode.js 二.使用 <!DOCTYPE html> <html lang="en"> <head> <me ...

  8. Android和IOS等效MD5加密

    最近在Android和IOS上都需要对用户的某些输入进行简单的加密,于是采用MD5加密方式. 首先将目的字符串加密一次,获得32位字符串 然后将32位字符串拆为2段,分别加密1次 最后将加密后的2段拼 ...

  9. asp访问数据库原理以及代码

    ActiveX Data Objects (ADO) 是一项容易使用并且可扩展的将数据库访问添加到 Web 页的技术.可以使用 ADO 去编写紧凑简明的脚本以便连接到 Open Database Co ...

  10. HDU 1176 免费馅饼【动态规划】

    解题思路:用a[i][j]表示在第i秒在地点j的掉落馅饼的数量,设整个馅饼掉落的时间持续为timemax,即为矩阵的高度,一共0到10个地点,为矩阵的长度,如图,即可构成数塔,因为考虑到在地点0的时候 ...