当前位置:网站首页>E - Apple Catching
E - Apple Catching
2022-06-26 12:40:00 【YJEthan】
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
* 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.
题目的大致意思是:有一只奶牛,有两颗神奇的苹果树,两颗苹果树每秒都有苹果掉下,一开始奶牛在1树下,奶牛最多移动m下,问奶牛最多能吃到多少个苹果
j%2+1表示当前奶牛所在的位置
dp[I][j]表示前I秒移动j次奶牛最多吃了多少个苹果
如果a[I]==j%2+1 dp[I][j]=max(dp[I-1][j-1],dp[I-1][j])+1
如果a[I]!=j%2+1 dp[I][j]=max(dp[I-1][j-1],dp[I-1][j])
#include<stdio.h>
#include<string.h>
int max(int a,int b)
{
if(a>b) b=a;
return b;
}
int main()
{
int i,j,t,w;
int dp[1005][33];
int a[1005];
while(scanf("%d%d",&t,&w)!=EOF)
{
for(i=1;i<=t;i++)
{
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp));
for(i=1;i<=t;i++)
{
dp[i][0]=dp[i-1][0];
if(a[i]==1) dp[i][0]++;
for(j=1;j<=w&&j<=i;j++)
{
if(a[i]==j%2+1)
{
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+1;
}
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
}
}
int max0=dp[t][0];
for(i=1;i<=w;i++)
{
max0=max(max0,dp[t][i]);
}
printf("%d\n",max0);
}return 0;
}
边栏推荐
猜你喜欢

Redis learning - 05 node JS client operation redis and pipeline pipeline

This function has none of deterministic, no SQL solution

ES6:Map

How does easygbs solve the abnormal use of intercom function?

Tiger DAO VC产品正式上线,Seektiger生态的有力补充
![HDU1724[辛普森公式求积分]Ellipse](/img/57/fb5098e150b5f3d91a5d0983a336ee.png)
HDU1724[辛普森公式求积分]Ellipse

Electron official docs series: Processes in Electron

Software testing - Fundamentals

The El form item contains two inputs. Verify the two inputs

Verilog中的系统任务(显示/打印类)--$display, $write,$strobe,$monitor
随机推荐
Adobe Acrobat prevents 30 security software from viewing PDF files or there are security risks
MySQL 自定义函数时:This function has none of DETERMINISTIC, NO SQL 解决方案
文件远程同步、备份神器rsync
Deep parsing MySQL binlog
. Net Maui performance improvement
倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
Electron official docs series: Best Practices
Accumulation of interview questions
el-form-item 包含两个input, 校验这两个input
Vivado 错误代码 [DRC PDCN-2721] 解决
OPLG: 新一代云原生可观测最佳实践
Redis learning - 04 persistence
Electron official docs series: Development
第01章_Linux下MySQL的安装与使用
P2393 yyy loves Maths II
[geek challenge 2019] rce me 1
四类线性相位 FIR滤波器设计 —— MATLAB源码全集
初识-软件测试
Mongodb of NoSQL - 03 mongodb CRUD
【网络是怎么连接的】第一章:浏览器生成消息