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/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...
随机推荐
- expect脚本中,变量的写法
一.expect脚本中,变量的不同写法 shell脚本中定义时间变量的写法:time=`date "+%Y%m%d"` ==>>直接照搬到expect中,设置的变量是不 ...
- 2018.6.27 Ajax实现异步刷新
Servlet获取URL地址.在HttpServletRequest类里,有以下六个取URL的函数: getContextPath 取得项目名 getServletPath 取得Servlet名 ge ...
- 完全用 Linux 工作
GNU/Linux 不是每個人都想用的.如果你只需要處理一般的事務,玩遊戲,那就不需要了解 Linux. UNIX 比 Windows 更適合用於科學研究工作. 大多數科學家和工程師以 UNIX 作為 ...
- 标准输入输出 stdio 流缓冲 buffering in standard streams
From : http://www.pixelbeat.org/programming/stdio_buffering/ 译者:李秋豪 我发现找出标准流用的是什么缓冲是一件困难的事. 例如下面这个使用 ...
- AngularJs学习笔记-数据绑定、管道
数据绑定.管道 (1)数据绑定(Angular中默认是单向绑定) 1.[]方括号 可以用于子组件传值 由于是单向绑定,所以当子组件中的iStars属性发生改变时,不会影响到父组件中product.ra ...
- Uva 填充正方形
暴力出奇迹 #include<iostream> #include<cstdio> using namespace std; +; int T,n; char S[maxn][ ...
- Linux下重要日志及查看方式
1.Linux下重要日志文件介绍 /var/log/boot.log 该文件记录了系统在引导过程中发生的事件,就是Linux系统开机自检过程显示的信息,如图1所示: 图1 /var/log/boot. ...
- Android_组件_Activity基础
一.概述 Activity是应用组件,提供了用户交互的窗口.一个应用由多个彼此联系的Activity组成.它大多数情况是全屏窗口显示,也可以作为悬浮窗口 或者 多窗口模式. 二.生命周期 下图是来自A ...
- GNU汇编 程序状态字访问指令
.text .global _start _start: mrs r0,cpsr orr r0,#0b100 msr cpsr,r0
- ubuntu18.04 and Linux mint 19安装virtualbox
1.1 安装Virtualbox root@amarsoft-ZHAOYANG-K43c-:~# apt-get install virtualbox -y 1.2 显示Virtualbox桌面图 ...