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/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...
随机推荐
- 快速开发一个PHP扩展
快速开发一个PHP扩展 作者:heiyeluren时间:2008-12-5博客:http://blog.csdn.net/heiyeshuwu 本文通过非常快速的方式讲解了如何制作一个PHP 5.2 ...
- 小弟在研究CUDA时出现一个问题,求解
这是<GPU高性能编程CUDA中文实战>中的例子,第七章,热传导模拟,但是出现下面的问题,求牛人解读.小弟跪谢... 主要问题就是关键字变白. 但是添加需要的头文件后一些系统自带的关键字也 ...
- 第十二篇、OC_仿淘宝商品详情页的翻页
// // GFBProductViewController.m // elmsc // // Created by MAC on 2016/11/26. // Copyright © 2016年 G ...
- 【思维题 经典模型】cf632F. Magic Matrix
非常妙的经典模型转化啊…… You're given a matrix A of size n × n. Let's call the matrix with nonnegative elements ...
- 本地已经存在的项目如何跟github发生关联
切换到本地项目地址 git init 初始化项目.该步骤会创建一个 .git文件夹是附属于该仓库的工作树. git add . git commit -am 'initial commit' git ...
- ZendFramework-2.4 源代码 - 整体架构(类图)
- 2019年Vue学习路线图
https://juejin.im/entry/5c108864f265da61726555ed 官网: https://cn.vuejs.org/index.html js引入地址 https:// ...
- 致敬wusir懒孩子自有懒孩子的生存之道之二
https://www.cnblogs.com/wupeiqi/ https://www.cnblogs.com/Eva-J/ https://www.cnblogs.com/wupeiqi/p/90 ...
- python中函数返回多个值
用ipython运行情况如下: # 新建一个函数 In [1]: def nums(): ...: a = 11 ...: b = 22 ...: c = 33 ...: return [a,b,c] ...
- Java消息中间件--初级篇
一. 为什么使用消息中间件? 假设用户登录系统 传统方式 用户登录 调用短息服务 积分服务 日志服务等各种服务 如果短息服务出现问题就无法发送短信而且用户登录成功必须所有调用全部完成返回 ...