概述
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) .
题目大意:
给一串序列,可以改变操作的方向(不能插入删除),最小化所修改字符的最大位置差。
code:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int SIZE = 200000+5;
char s[SIZE];
int ex,ey;
int xSum[SIZE];
int ySum[SIZE];
int dx[200],dy[200];
bool judge(int n, int len, int ex,int ey){
for(int i = 1; i + len-1 <= n; ++i){
int curx = xSum[i-1] + xSum[n]-xSum[i+len-1]; // 去除长度 len长度 后的 x 走到的位置
int cury = ySum[i-1] + ySum[n]-ySum[i+len-1]; // 去除长度 len 长度后的 y走到的位置
int delta = abs(curx-ex) + abs(cury-ey); // 距离到达终点还需要多少步
if(delta <= len && (len-delta)%2 == 0) // 到达终点还需要的步数 一定小于 目前可以通过改变方向的那些步数的个数 len
return true;
// 并且 因为此时 len两端 必须是改变的(0 或者 1 是特殊情况) len与delta差值 必须为偶数才能到终点
}
return false;
}
/*
二分法
*/
int main()
{
int n;
dy['U'] = 1;
dy['D'] = -1;
dx['L'] = -1;
dx['R'] = 1;
scanf("%dn%s",&n,s+1);
scanf("%d%d",&ex,&ey);
for(int i = 1; i <= n; ++i){
xSum[i] = xSum[i-1] + dx[s[i]]; //操作i后x的位置
ySum[i] = ySum[i-1] + dy[s[i]]; //操作i后y所在的位置
}
int lb = 0,ub = n;
int ans = -1,mid;
while(lb <= ub){
mid = (lb+ub)/2;
if(judge(n,mid,ex,ey)){
ans = mid;
ub = mid-1;
}
else{
lb = mid+1;
}
}
printf("%dn",ans);
return 0;
}
最后
以上就是寂寞铃铛为你收集整理的CF 2018_10_25 C. Vasya and Robot (二分)的全部内容,希望文章能够帮你解决CF 2018_10_25 C. Vasya and Robot (二分)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复