H - 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

5
RURUU
-2 3

Output

3

Input

4
RULR
1 1

Output

0

Input

3
UUU
100 100

Output

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

#include<bits/stdc++.h>
using namespace std;
int x[200002];
int y[200002];
char s[200002];
int n;
int ex,ey;
bool check(int len){
	int sx=x[n]; int sy=y[n];
	for(int i=1;i+len-1<=n;i++){
		int mox=x[i+len-1]-x[i-1];
		int moy=y[i+len-1]-y[i-1];
		int di=abs(sx-mox-ex)+abs(sy-moy-ey);
		
		if(di<=len&&(len-di)%2==0) return true;
		
	}
	return false;
}
int main(){

	cin>>n;
	scanf("%s",s+1);
	cin>>ex>>ey;
	for(int i=1;i<=n;i++){
		if(s[i]=='U'){
			y[i]=1;
		}
		if(s[i]=='D'){
			y[i]=-1;
		}
		if(s[i]=='L'){
			x[i]=-1;
		}
		if(s[i]=='R'){
			x[i]=1;
		}
	}
	for(int i=1;i<=n;i++){
		x[i]+=x[i-1];
		y[i]+=y[i-1];
	}
	int l=0; int r=n;int ans=0;bool flag=false;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid)){
			ans=mid;
			r=mid-1;
			flag=true;
		}
		else l=mid+1;
	}
	if(!flag) cout<<-1<<endl;
	else cout<<ans<<endl;
	return 0;
	
	
	
}

这道题目,至少用了三个小时,无语。

一开始我把他左右上下的能抵消就抵消,但是是要用一段的和的,也就是要用前缀和,我太草率了。

然后,要注意,几何意义,公式表示的就是连续的一个长度。

二分,降复杂度,这回真是刻骨铭心了。

修改相当于去掉再添加,只不过数量是一样的而已,而我想直接做的话就不是很好下手了,因为原来的已经知道,删掉就容易得知目前的坐标,然后和目标的不同,要会利用不等式,在一个范围,而且两边是一定要改变的,这个限制不可忽略,接下来就是凑等式要满足的条件,奇偶性一样之类的。

还改了很久的细节,比如,字符串下标从1开始,方便前缀和之类的计算。

110个测试点挺吓人的了。

还有len可以为0,这个很重要,可以不变,题目中的特殊给忘了。

所以二分的时候要注意开始,l=0,而非l=1。

猜你喜欢

转载自blog.csdn.net/qq_42865713/article/details/86773389