当前位置:网站首页>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;
}
边栏推荐
- Software testing - Fundamentals
- openlayers 绘制动态迁徙线、曲线
- 【网络是怎么连接的】第一章:浏览器生成消息
- 倍福NC轴状态转移图解析
- Redis learning - 01 introduction, installation and configuration
- find及du -sh显示权限不够的解决方法
- MySQL 自定义函数时:This function has none of DETERMINISTIC, NO SQL 解决方案
- Source code learning: atomicinteger class code internal logic
- Echart堆叠柱状图:色块之间添加白色间距效果设置
- processsing 函数random
猜你喜欢

详细讲解C语言10(C语言系列)

Mongodb of NoSQL - 03 mongodb CRUD

倍福TwinCAT3 NCI在NC轴界面中的基本配置和测试

【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接

【网络是怎么连接的】第二章(下):一个网络包的接收

Group counting practice experiment 9 -- using cmstudio to design microprogram instructions based on segment model machine (2)

倍福Ethercat模块网络诊断和硬件排查的基本方法
Adobe Acrobat阻止30款安全软件查看PDF文件 或存在安全风险

goto语句实现关机小程序

倍福PLC基于NT_Shutdown实现控制器自动关机重启
随机推荐
Adobe Acrobat阻止30款安全软件查看PDF文件 或存在安全风险
postgis計算角度
Websocket and socket IO case practice
倍福PLC基于CX5130实现数据的断电保持
软件测试 - 基础篇
Deep parsing MySQL binlog
软件测试测试常见分类有哪些?
机组实践实验8——使用CMStudio设计基于基本模型机微程序指令(1)
Electron official docs series: Processes in Electron
Software testing - concept
源码学习:AtomicInteger类代码内部逻辑
Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan
Learning Processing Zoog
Electron official docs series: Get Started
Summary of some application research cases of UAV Remote Sensing in forest monitoring
Software testing - Fundamentals
Typescript
Record a phpcms9.6.3 vulnerability to use the getshell to the intranet domain control
processing 函数translate(mouseX, mouseY)学习
心脏滴血漏洞(CVE-2014-0160)分析与防护