Educational Codeforces Round 53 (Rated for Div. 2)C. Vasya and Robot(二分)

                                                                      C. Vasya and Robot

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)到达的终点(sx,sy),并求一下区间指令偏移量的前缀和,这样可以轻松得到任意两点的偏移量

二、根据枚举的区间长度枚举区间左端点设为i,区间长度为len,那么右端点就为i+len-1,根据左右端点算出改区间产生的偏移量,

如果计为(mx,my),那么删除区间后的位置就为(sx-mx,sy-my),算出该点到终点的距离dis,如果len>=dis(len>dis表示有多余的指令,但成对出现可抵消)并且len和dis同奇偶,那么改区间长度就是一个可能的答案,由于要找最小的,所以要把整个区间【1,n】都要二分,最后的值一定是最小的,时间复杂度nlogn

好了,废话不多说了,直接上代码

ac code:

#include
using namespace std;
const int maxn=2e5+2;
int x[maxn],y[maxn];
int n,ex,ey;
bool check(int len)
{
    int sx=x[n],sy=y[n];
    for(int i=1;i+len-1<=n;i++){//枚举区间左端点
        int l=i,r=i+len-1;
        int mx=x[r]-x[l-1];
        int my=y[r]-y[l-1];
        int tx=sx-mx,ty=sy-my;
        int dis=abs(tx-ex)+abs(ty-ey);
        if(len>=dis&&(len%2==dis%2)) return true;
    }
    return false;
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    string s;
    cin>>n>>s>>ex>>ey;
    s=" "+s;
    for(int i=1; i<=n; i++)
        if(s[i]=='U')
            y[i]=1;
        else if(s[i]=='D')
            y[i]=-1;
        else if(s[i]=='L')
            x[i]=-1;
        else x[i]=1;
    for(int i=1;i<=n;i++)
    {
        x[i]=x[i-1]+x[i];
        y[i]=y[i-1]+y[i];///前缀和
    }
    int l=0,r=n+1;
    int ans=-1;
    while(l<=r)//二分区间长度
    {
        int mid=(l+r)>>1;
        if(check(mid))
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    printf("%d\n",ans);
    return 0;
}

 

你可能感兴趣的:(数学,思维题)