CF 1073C 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:ide

  • U — move from (x,y)(x,y) to (x,y+1)(x,y+1);
  • D — move from (x,y)(x,y) to (x,y1)(x,y−1);
  • L — move from (x,y)(x,y) to (x1,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).this

Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: maxIDminID+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 2255 and 77 are changed, so the length of changed subsegment is 72+1=67−2+1=6. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is 11.spa

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

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

Input

The first line contains one integer number n (1n2105)n (1≤n≤2⋅105) — the number of operations.ci

The second line contains the sequence of operations — a string of nn characters. Each character is either UDL or R.rem

The third line contains two integers x,y (109x,y109)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.input

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 31+1=33−1+1=3.string

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)出發,輸入一段指令字符串,和機器人須要在指定步數後到達的終點,問若是機器人須要在指定步數內到達終點,那麼須要對原指令字符串作出怎樣的改變,假設改變 字符串的最大下標爲maxindex,改變字符串的最小下標爲minindex,輸出最小的 maxindex-minindex+1,即,輸出最小的改變字符串的連續區間長度(該區間內的字符不必定要所有發生改變)

 

【分析】

首先考慮在什麼狀況下,不管如何改動這個字符串都不能到達指定位置

1、字符串長度小於從原點到指定位置的距離

2、字符串長度與從原點到指定位置的奇偶性不一樣

在除去這兩種狀況下,剩餘的狀況都必定有答案。鑑於其可能解時連續的整數,所以,能夠用二分枚舉全部可能,進而找出最小的連續區間長度。

應注意,當根據給定字符串移動就能到達指定位置,即最小區間爲0時,應排除在二分枚舉的狀況以外。

實際寫代碼時,特殊狀況能夠被包含於普通狀況。但能夠做爲思路的引子。

當枚舉長度爲 x 時,考慮在 string 中全部長度爲 x 的子串,是否存在一個子串可行。若存在,嘗試縮短子串長度;若不存在,延長子串長度。

 

判斷子串是否可行的方法:

設全集爲給定字符串,沿着子串的補集移動,記這樣移動到的點爲 pos 。求 pos 到 指定位置 的距離,記爲 d ,記子串的長度爲 len。知足以下兩種狀況,則子串可行。

1d <= len

2(len-d)%2==0

 

【代碼】

#include<cstdio>
#include<cstdlib>
using namespace std;
const int N=2e5+5;
int n,ex,ey,sx[N],sy[N];char s[N];
inline bool check(int m){//假定最佳區間長度爲m 
	for(int i=1;i+m-1<=n;i++){
		int decx=sx[n]-sx[i+m-1]+sx[i-1];
		int decy=sy[n]-sy[i+m-1]+sy[i-1];
		//不須要改變的區間恆存在的貢獻
		int nedx=ex-decx;
		int nedy=ey-decy;
		//須要改變的區間中,x和y想要到達終點,所需剛好做出的貢獻
		if(abs(nedx)+abs(nedy)<=m&&!(m-abs(nedx)-abs(nedy)&1)) return 1;
		//(abs(tx)+abs(ty)位字符作出使該人恰好到達終點的貢獻,
		//剩下位的字符若是是偶數,就可讓其多走的路程兩兩抵消,從而恰好到達終點
	}
	return 0;
}
int main(){
	scanf("%d%s%d%d",&n,s+1,&ex,&ey);
	for(int i=1;i<=n;i++){
		sx[i]=sx[i-1]+(s[i]=='L'?-1:(s[i]=='R'?1:0));
		sy[i]=sy[i-1]+(s[i]=='D'?-1:(s[i]=='U'?1:0));
	}
	int l=0,r=n,mid,ans=-1;
	while(l<=r){
		mid=l+r>>1;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}
		else{
			l=mid+1;
		}
	}
	printf("%d\n",ans);
	return 0;
}
相關文章
相關標籤/搜索