CF1073C.Vasya and Robot
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell (0,0). Robot can perform the following four kinds of operations:
- U — move from (x,y) to (x,y+1);
- D — move from (x,y) to (x,y−1);
- L — move from (x,y) to (x−1,y);
- R — move from (x,y) to (x+1,y).
Vasya also has got a sequence of n operations. Vasya wants to modify this sequence so after performing it the robot will end up in (x,y).
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: maxID−minID+1, where maxID is the maximum index of a changed operation, and minID is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices 2, 5 and 7 are changed, so the length of changed subsegment is 7−2+1=6. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is 1.
If there are no changes, then the length of changed subsegment is 0. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from (0,0) to (x,y), or tell him that it's impossible.
瓦西娅有一个机器人,初始位于无限笛卡尔平面上的格点 (0,0) 处。机器人可执行以下四种操作:
- U — 从 (x,y) 移动到 (x,y+1);
- D — 从 (x,y) 移动到 (x,y−1);
- L — 从 (x,y) 移动到 (x−1,y);
- R — 从 (x,y) 移动到 (x+1,y)。
瓦西娅还拥有一段长度为 n 的操作序列。他希望修改该序列,使得执行修改后的序列后,机器人最终恰好停在目标位置 (x,y)。
瓦西娅希望被修改的操作构成的连续子段长度最小。该长度定义为:maxID−minID+1,其中 maxID 是所有被修改操作中最大的下标,minID 是所有被修改操作中最小的下标。例如,若瓦西娅将 RRRRRRR 修改为 RLRRLRL,则下标为 2、5 和 7 的操作被修改(假设下标从 1 开始),因此被修改子段的长度为 7−2+1=6。再如,若将 DDDD 修改为 DDRD,则仅下标 3 处的操作被修改,因此被修改子段长度为 1。
若无需任何修改,则被修改子段长度为 0。所谓“修改一个操作”,是指将其替换为某一种操作(可以是它本身);瓦西娅不能向序列中插入新操作,也不能删除已有操作。
请帮助瓦西娅!告诉他:为使机器人从 (0,0) 出发最终到达 (x,y),所需修改的最短连续子段长度是多少;若不可能实现,请说明不可能。
输入格式
The first line contains one integer number n (1≤n≤2⋅105) — the number of operations.
The second line contains the sequence of operations — a string of n characters. Each character is either U, D, L or R.
The third line contains two integers x,y (−109≤x,y≤109) — the coordinates of the cell where the robot should end its path.
第一行包含一个整数 n (1≤n≤2⋅105) —— 操作的次数。
第二行包含操作序列 —— 一个长度为 n 的字符串。每个字符为 U、D、L 或 R 中的一个。
第三行包含两个整数 x,y (−109≤x,y≤109) —— 机器人路径终点所在单元格的坐标。
输出格式
Print one integer — the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from (0,0) to (x,y). If this change is impossible, print −1.
输出一个整数——为使机器人从 (0,0) 移动到 (x,y),所需修改的最短子段长度。如果无法实现,则输出 −1。
输入输出样例
输入#1
5 RURUU -2 3
输出#1
3
输入#2
4 RULR 1 1
输出#2
0
输入#3
3 UUU 100 100
输出#3
-1
说明/提示
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is 3−1+1=3.
In the second example the given sequence already leads the robot to (x,y), so the length of the changed subsegment is 0.
In the third example the robot can't end his path in the cell (x,y).
在第一个例子中,序列可以更改为 LULUU。因此被更改的子段长度为 3−1+1=3。
在第二个例子中,给定的序列已能使机器人到达 (x,y),因此被更改的子段长度为 0。
在第三个例子中,机器人无法在其路径终点到达单元格 (x,y)。
输入解题思路,AI测评打分。不知道怎么写?