C. Vasya and Robot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell (0,0)(0,0). Robot can perform the following four kinds of operations:
- U — move from (x,y)(x,y) to (x,y+1)(x,y+1);
- D — move from (x,y)(x,y) to (x,y−1)(x,y−1);
- L — move from (x,y)(x,y) to (x−1,y)(x−1,y);
- R — move from (x,y)(x,y) to (x+1,y)(x+1,y).
Vasya also has got a sequence of nn operations. Vasya wants to modify this sequence so after performing it the robot will end up in (x,y)(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+1maxID−minID+1, where maxIDmaxID is the maximum index of a changed operation, and minIDminID is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices 22, 55 and 77 are changed, so the length of changed subsegment is 7−2+1=67−2+1=6. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is 11.
If there are no changes, then the length of changed subsegment is 00. 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)(0,0) to (x,y)(x,y), or tell him that it's impossible.
Input
The first line contains one integer number n (1≤n≤2⋅105)n (1≤n≤2⋅105) — the number of operations.
The second line contains the sequence of operations — a string of nn characters. Each character is either U, D, L or R.
The third line contains two integers x,y (−109≤x,y≤109)x,y (−109≤x,y≤109) — the coordinates of the cell where the robot should end its path.
Output
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)(0,0) to (x,y)(x,y). If this change is impossible, print −1−1.
Examples
input
Copy
5 RURUU -2 3
output
Copy
3
input
Copy
4 RULR 1 1
output
Copy
0
input
Copy
3 UUU 100 100
output
Copy
-1
Note
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is 3−1+1=33−1+1=3.
In the second example the given sequence already leads the robot to (x,y)(x,y), so the length of the changed subsegment is 00.
In the third example the robot can't end his path in the cell (x,y)(x,y).
题目大意:有一个机器人在一个坐标系里的位置(0, 0),有四种操作U,D,L,R分别代表向上走,向下走,向左走,向右走,给定一个由这四种操作组成的操作序列,问能否通过修改这个操作序列的某些位置使得机器人最后到达点A(x, y),如果可以的话输出最小的改变区间,改变区间为最大的修改位置到最小的修改位置这一区间。
每次二分区间长度,然后去每次枚举区间位置,因为操作的先后顺序无影响,所以将原有操作区间去掉之后机器人将到达点B(x1, y1),如果从A点到B点的步数正好等于当前二分的区间长度,那么可行,如果步数大于区间长度且差值为2的整数倍的话那么也可行,因为可以来回走。
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 2e5 + 10;
int x[N], y[N];
char s[N];
int tx, ty, len;
int check(int mid) {
for (int i = 1; i + mid - 1 <= len; i++) {
int j = i + mid - 1;
int a = x[len] - x[j] + x[i - 1];
int b = y[len] - y[j] + y[i - 1];
int xx = abs(tx - a) + abs(ty - b);
if (mid >= xx && (mid - xx) % 2 == 0) return 1;
}
return 0;
}
int main() {
scanf("%d", &len);
scanf("%s", s);
scanf("%d%d", &tx, &ty);
x[0] = y[0] = 0;
if (abs(tx) + abs(ty) > len) {printf("-1\n");return 0;}
int xx = len - abs(tx) - abs(ty);
if (xx != 0 && xx % 2 != 0) {printf("-1\n");return 0;}
for (int i = 0; i < len; i++) {
if (s[i] == 'L') {
x[i + 1] = x[i] - 1;
y[i + 1] = y[i];
}
if (s[i] == 'R') {
x[i + 1] = x[i] + 1;
y[i + 1] = y[i];
}
if (s[i] == 'U') {
y[i + 1] = y[i] + 1;
x[i + 1] = x[i];
}
if (s[i] == 'D') {
y[i + 1] = y[i] - 1;
x[i + 1] = x[i];
}
}
if (x[len] == tx && y[len] == ty) {printf("0\n");return 0;}
int l = 1, r = len;
int res;
while (l <= r) {
int mid = (l + r) / 2;
int ret = check(mid);
if (ret) {
res = mid;
r = mid - 1;
}
else l = mid + 1;
}
printf("%d\n", res);
return 0;
}