Codeforces 1073C: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)$. 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 nn 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.

Input

The first line contains one integer number $n (1≤n≤2⋅10^5)$ — 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 (−10^9≤x,y≤10^9)$ — 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)$ to $(x,y)$. If this change is impossible, print $−1$.

Examples

input

1
2
3
5
RURUU
-2 3

output

1
3

input

1
2
3
4
RULR
1 1

output

1
0

input

1
2
3
3
UUU
100 100

output

1
-1

Note

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)$.

题意

一个机器人从$(0,0)$点出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假设改变 字符串的最大下标为$maxID$ ,改变字符串的最小下标为$minID$ ,输出最小的 $maxID-minID+1​$ ,即,输出最小的改变字符串的连续区间长度(该区间内的字符不一定要全部发生改变)

Solve

字符串长度小于原点到指定位置的距离,字符串长度与原点到指定位置具有不同的奇偶性。在这两种情况下,是无论如何都无法到达指定位置的。其余情况都一定有答案。

因为是要求区间的长度,所以二分枚举区间长度,对于每个区间长度尺取,找出所有可达的情况。如果某个区间长度可行,尝试去缩小当前区间长度;否则,延长区间长度

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*************************************************************************
> File Name: C.cpp
> Author: WZY
> Created Time: 2019年02月15日 15:39:58
************************************************************************/

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
char ch[maxn];
int sumx[maxn],sumy[maxn];
int n;
int x,y;
// 判断区间是否可行
bool check(int len)
{
for(int l=1;l+len-1<=n;l++)
{
int r=l+len-1;
// 不需要改变的指令个数
int _x=sumx[l-1]+sumx[n]-sumx[r];
int _y=sumy[l-1]+sumy[n]-sumy[r];
// 计算当前点到指定点的距离
int sum=abs(_x-x)+abs(_y-y);
// 当前点到指定点的距离<=len并且多走的路程可以两两抵消
if(sum<=len&&(len-sum)%2==0)
return true;
}
return false;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>(ch+1);
cin>>x>>y;
for(int i=1;i<=n;i++)
{
if(ch[i]=='R')
sumx[i]=sumx[i-1]+1,sumy[i]=sumy[i-1];
else if(ch[i]=='L')
sumx[i]=sumx[i-1]-1,sumy[i]=sumy[i-1];
else if(ch[i]=='U')
sumx[i]=sumx[i-1],sumy[i]=sumy[i-1]+1;
else
sumx[i]=sumx[i-1],sumy[i]=sumy[i-1]-1;
}
int ans,l,r;
l=0,r=n;
ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
ans=mid,r=mid-1;
else
l=mid+1;
}
cout<<ans<<endl;
return 0;
}

本文标题:Codeforces 1073C:Vasya and Robot(二分)

文章作者:执念

发布时间:2019年02月15日 - 22:02

最后更新:2019年02月15日 - 22:02

原始链接:https://blog.wzy1999.wang/solve/cf1073C/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------
0%