ABC128F Frog Jump
题目大意
给定一个长为 $n$ 的数组 $s$,下标从 $0$ 开始。$ 3 \le n \le 10^5$,$-10^9 \le s_i \le 10^9$,$s_0 = s_{n - 1} = 0$ 。
一只青蛙要在数组 $s$ 上玩一个游戏。游戏规则如下
初始时青蛙的位置(即青蛙所在的数组下标)是 $0$,它的分数是 $0$;青蛙要选择两个正整数 $A,B$,并不断重复下述过程直到游戏结束:
step 1. 设青蛙当前位置是 $x$,跳到 $y = x + A$ 。若 $y = n -1$,游戏结束;若 $y$ 超出下标范围或者青蛙之前到过位置 $y$,则游戏结束,分数是 $-\infty$;否则分数加上 $s_{y}$,进行 step 2。
step 2. 设青蛙当前位置是 $x$,跳到 $y = x - B$ 。 若 $y = n -1$,游戏结束;若 $y$ 超出下标范围或者青蛙之前到过位置 $y$,则游戏结束,分数是 $-\infty$;否则分数加上 $s_{y}$,回到 step 1。
试问青蛙的游戏得分最大可能是多少?
分析
比赛时我把题意误解成每次选择 step 1 或 step 2 其中之一进行操作。
$A = n - 1$ 是平凡情形,考虑青蛙至少跳两步且得分不是 $-\infty$ 的情形。
此时有(i) $ 1 \le B < A \le n - 2$;(ii)青蛙的位置序列是 $0, A, A - B, (A - B) + A, 2(A- B), 2(A- B) + A, \dots, k(A- B), k(A-B) + A = n - 1$,且序列中无重复元素。
key observation
这个序列可以分解成两部分,从左往右,$0, A - B, 2(A-B), \dots, k(A-B)$;从右往左,$n - 1 = k(A - B) + A, n - 1 -(A-B), n- 1- 2(A-B), \dots, n - 1 - k(A- B) = A$ 。
换言之,青蛙的位置序列可以分解成两个由数组 $s$ 的下标构成的不相交(即无共同元素)的等差序列,一个从左往右,首项是 0,公差是 $A - B$;另一个是从右往左,首项是 $n - 1$,公差是 $B - A$ 。
容易看出,青蛙的位置序列完全由 $A - B$ 和 $k$ 决定。于是我们可以得到一个解法:
枚举 $A - B$ 的值;固定 $A-B$,再枚举 $k$ 的值,若两等差序列无共同元素,则更新答案。
ABC128F Frog Jump的更多相关文章
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- Leetcode: Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [Swift]LeetCode403. 青蛙过河 | Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [leetcode]403. Frog Jump青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- LeetCode403. Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] 403. Frog Jump 青蛙跳
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- div 3 frog jump
There is a frog staying to the left of the string s=s1s2…sn consisting of n characters (to be more p ...
- [leetcode] 403. Frog Jump
https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...
随机推荐
- "segmentation fault " when "import tensorflow as tf"
https://github.com/tensorflow/tensorflow/issues/2034
- Echarts样式
Echarts设置样式如下 <div id="main" style="width: 250px;height:200px;"></div&g ...
- iOS开发遇到的坑之七--上传app Stroe被拒绝:The app references non-public symbols in : _UICreateCGImageFromIOSurface
这是上学期遇到的问题了,突然查阅邮箱的时候发现了,遂在这里记录下来,希望大家以后注意这个问题 我上传App Store的时候,apple给我发的邮件原文: Dear developer, We hav ...
- ES6 Proxy拦截器详解
Proxy 拦截器 如有错误,麻烦指正,共同学习 Proxy的原意是"拦截",可以理解为对目标对象的访问和操作之前进行一次拦截.提供了这种机制,所以可以对目标对象进行修改和过滤的操 ...
- 洛谷P3374树状数组1
下有彩蛋(from https://www.cnblogs.com/wuwangchuxin0924/p/5921130.html)树状数组的blog写的最好的是这位//https://www.cnb ...
- python字典形list 去重复
data_list = [{"}] run_function = lambda x, y: x if y in x else x + [y] return reduce(run_functi ...
- Python知识点入门笔记——特色数据类型(集合)
集合是一种不重复的无序集 集合用花括号来定义{} 集合和字典一样,里面的顺序是无序的,{1,2,3}和{3,2,1}是相等的 集合的元素不可重复,也就是说{1,2,2,3}是不存在的,应该写为{1,2 ...
- Mongodb内嵌对象关联查询
db.-10-30T00:00:00Z"),"$lt":ISODate("2018-10-30T23:59:00Z")}, "equip.$ ...
- 关于web.xml配置中的<url-pattern>
标签<url-pattern> <url-pattern>是我们用Servlet做Web项目时需要经常配置的标签,例: <servlet><servlet-n ...
- linux下的source命令
Linux Source命令及脚本的执行方式解析 当我修改了/etc/profile文件,我想让它立刻生效,而不用重新登录:这时就想到用source命令,如:source /etc/profile ...