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/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边 ...
随机推荐
- Airflow 调度基础
1. Airflow Airflow是一个调度.监控工作流的平台.用于将一个工作流制定为一组任务的有向无环图(DAG),并指派到一组计算节点上,根据相互之间的依赖关系,有序执行. 2. 安装 pip安 ...
- 2018.6.22 Java试题测试结果
如何从有数字规律的网址抓取网页并保存在当前目录?假设网址为 http://test/0.xml,其中这个数字可以递增到100. for((i=0;i<100;++i));do wget http ...
- python_30_购物车复习
prodcut_list=[ ('Iphone', 5800), ('Mac Pro', 9800), ('Bike', 800), ('Watch', 10600), ('Coffee', 31), ...
- spring-boot自定义启动端口
有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...
- C# 常用函数和方法集汇总
1.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.D ...
- 零基础快速入门SpringBoot2.0教程 (二)
一.SpringBoot2.x使用Dev-tool热部署 简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用 官方地址:https://docs.spring. ...
- tomcat服务器用Servlet类查找磁盘文件上的Json信息,如果匹配则在浏览器上显示出该条内容的全部信息
package com.swift; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOE ...
- es6中的模版字符串
目录 字符串拼接 includes() startsWith() endsWith() padStart() es6中的模版字符串替代了原有的字符串拼接功能. 字符串拼接 es5方式 传统的字符串拼接 ...
- 【点分树】codechef Yet Another Tree Problem
已经连咕了好几天博客了:比较经典的题目 题目大意 给出一个 N 个点的树和$K_i$, 求每个点到其他所有点距离中第 $K_i$ 小的数值. 题目分析 做法一:点分树上$\log^3$ 首先暴力做法: ...
- yii2 基本的增删改查
一:添加方法 1.1 使用成员属性的方式 save $user_name = $_POST['user_name']; $password = $_POST['password']; //实例化 $u ...