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。

 

### 关于 Vasya 和多重集的编程竞赛算法题目解决方案 #### 题目描述 给定一个多重集合 `s`,目标是将其分割成两个新的多重集合 `a` 和 `b` (其中一个可以为空),使得这两个新集合中的“好数”的数量相等。“好数”定义为在一个特定多集中恰好只出现一次的数字。 为了实现这一目标,需要考虑如何有效地统计并分配这些元素到不同的子集中去[^1]。 #### 解决思路 一种有效的解决方法是从输入数据的特点出发思考。如果某个数值在整个原始集合中出现了偶数次,则该值可以在不影响最终结果的情况下被平均分入两个子集中;而对于那些仅出现奇数次数的情况,则必须小心处理以确保能够达成平衡条件——即让尽可能多的不同类型的单例项分别进入各自的目标组内[^2]。 具体来说: - 对于任何频率大于等于两次(无论是奇还是偶)的数据点而言,总是能通过适当划分来满足上述要求; - 当遇到频度为一的情形时,就需要额外注意了:因为这直接影响着能否成功创建具有相同数目唯一成员的新分区。 因此,在实际编码过程中应该优先处理那些重复率较高的项目,并记录下所有独一无二实例的位置以便后续操作使用。 #### Python 实现代码示例 下面是一个基于此逻辑编写的Python函数,用于求解这个问题: ```python from collections import Counter def can_split_equally(s): count = Counter(s) # 统计各元素出现次数 singletons = sum(1 for v in count.values() if v == 1) return singletons % 2 == 0 # 测试用例 test_cases = [ [1, 2, 2, 3], # True 可以分成 {1} 和 {2, 2, 3} [1, 2, 3, 4, 5], # False 单独存在的数字有五个无法平分 ] for case in test_cases: print(f"Input: {case}, Can Split Equally? :{can_split_equally(case)}") ``` 这个程序首先利用 `collections.Counter` 来计算每个整数在列表里边出现过的总次数。接着它会遍历所有的键值对,累积起所有只出现过一次(也就是所谓的 “好数” 或者说是单一实例)的数量。最后一步就是判断这样的特殊案例是不是构成了一个偶数序列长度 —— 如果是的话就意味着存在至少一组可行解;反之则不存在这样的一分为二方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值