codeforces1073C. Vasya and Robot(二分+前缀和)

                                                                                                      C. Vasya and Robot

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

 

一、原题地址

点我传送

 

二、大致题意

给出一个机器人行走的路径,还有一个终点的坐标。现在可以修改机器人行走路径上的一段,询问最短修改长度为多少的路径,可以使机器人到达给定的终点坐标。不行则输出-1.

 

三、思路

机器人行走的每一步先后顺序其实是没有意义的,这也是这道题的关键。

正因为这一点,我们可以将每一步在坐标上的偏移量单独提取出来,然后用前缀和累加,这样的话就可以快速的得到在一段[ l , r ]区间上机器人实际偏移的坐标。

先找出机器人在无修改的情况下对于( 0,0 )的偏移记作sx , sy ,那么修改操作实际上就是先把一段区间内的偏移量删去,然后再加上我们想要加上的偏移量。

记去掉替换区间后的位置在nowx,nowy ,可以计算出该点到达终点的曼哈顿距离 dis 。想要保证我们枚举的区间符合条件就需要保证我们替换上去的区间长度 len 奇偶性和终点的曼哈顿距离 dis 一致,并且保证len>=dis。

至于机器人到底是怎么走的我们不需要考虑,因为一定是会有一种走法符合的。

以上操作是可以n^2实现的,当然是超时的,所以二分一下替换的区间长度就是nlogn的做法了。代码跑了31ms.

 

四、代码

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<set>
#include<map>
#include<unordered_set>
#include<vector>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long LL;


int n;
char s[200005];
int ex, ey;
int x[200005], y[200005];
void prework()
{
	scanf("%d", &n);
	scanf("%s", s + 1);
	scanf("%d %d", &ex, &ey);
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'R')
		{
			x[i] = 1; y[i] = 0;
		}
		else if (s[i] == 'L')
		{
			x[i] = -1; y[i] = 0;
		}
		else if (s[i] == 'U')
		{
			x[i] = 0; y[i] = 1;
		}
		else if (s[i] == 'D')
		{
			x[i] = 0; y[i] = -1;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		x[i] = x[i - 1] + x[i];
		y[i] = y[i - 1] + y[i];
	}
	return;
}
bool check(int len)
{
	int sx = x[n], sy = y[n];
	for (int i = 1; i + len - 1 <= n; i++)
	{

		int tl = i, tr = i + len - 1;
		int movex = x[tr] - x[tl - 1];		
		int movey = y[tr] - y[tl - 1];					//替换部分被去掉的偏移量
		int nowx = sx - movex, nowy = sy - movey;		//去掉替换部分后的所在位置
		int dis = abs(nowx - ex) + abs(nowy - ey);
		if (dis <= len && ((dis & 1) && (len & 1) || ((dis % 2 == 0) && (len % 2 == 0))))return true;
	}
	return false;
}
void Mainwork()
{
	int l = 0, r = n;
	int mid, ans;
	bool tag = false;
	while (l <= r)
	{
		mid = (l + r) >> 1;
		if (check(mid))
		{
			ans = mid;
			r = mid - 1;
			tag = true;
		}
		else l = mid + 1;
	}
	if(tag) printf("%d\n", ans);
	else printf("-1\n");
	return;
}
int main()
{
	prework();
	Mainwork();



	getchar();
	getchar();
	return 0;
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值