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<bits/stdc++.h>
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;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值