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

思路:这题二分长度就好了,可惜我一开始看成了只能上下或左右互换,就写炸了

做法:前缀和纪录向上和向右贡献,然后二分长度,对于长度len,枚举起点,假设起点i,终点j,j-i+1=len,那去掉区间[i,j]后计算走到了(x1,y1),然后s=abs(x1-x)+abs(y1-y)就是要修改的路程,那只要s<=len且(s-len)%2==0就可以修改完成,(s-len)%2==0的话,说明多余的字符可以来回走消耗掉

 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.

    Input

    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.

    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

    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=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)

    .
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define maxn 1005500
    ll sx[maxn],sy[maxn];int n,x,y;
    char a[maxn];
    int pd(int mid)
    {
        int x1=0,y1=0;
        for(int i=1;i+mid-1<=n;i++)
        {
            x1=sx[i-1]+sx[n]-sx[i+mid-1];
            y1=sy[i-1]+sy[n]-sy[i+mid-1];
            int s=abs(x1-x)+abs(y1-y);
            if(mid>=s&&(mid-s)%2==0) return 1;
        }
        return 0;
    }
    int main()
    {
    
        scanf("%d",&n);
        scanf("%s",a+1);
        scanf("%d %d",&x,&y);
        for(int i=1;i<=n;i++)
        {
            if(a[i]=='U') sy[i]=sy[i-1]+1;
            else if(a[i]=='D') sy[i]=sy[i-1]-1;
            else sy[i]=sy[i-1];
            if(a[i]=='R') sx[i]=sx[i-1]+1;
            else if(a[i]=='L') sx[i]=sx[i-1]-1;
            else sx[i]=sx[i-1];
        }
        int l=0,r=n,p=-1;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(pd(mid))
            {
                p=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%d\n",p);
    }
    

猜你喜欢

转载自blog.csdn.net/qq_43497140/article/details/106324874