概述
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).
题意:
一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数 n 后恰好到达的终点;
问 如果机器人需要在指定步数 n 到达终点,那么需要对原指令字符串做出怎样的改变,假设改变 字符串的最大下标为maxindex,改变字符串的最小下标为minindex,输出最小的 maxindex-minindex+1,即,输出最小的改变字符串的区间长度(该区间内的字符不一定要全部发生改变)。
解题分析:
本题用二分答案求解,先预处理得到x,y的前缀和,即原始指令字符串对x,y的改变所作出的贡献,然后就是二分答案了,二分最短区间的长度。当然,二分答案最重要的就是check()函数,枚举所有长度为mid的区间,二分区间内的每一个操作都可以做出一个关于x or y 位置上的贡献;利用前缀和计算出 最终目标x y 与 去掉该区间xx,xy 相差的距离 tx ty 即为该区间恰好所需的改变,然后判断该区间是否能够符合,具体步骤见代码。
#include <bits/stdc++.h>
using namespace std;
const int N =1e6+10;
int n,x,y;
char s[N];
int sx[N],sy[N];
bool check(int m){
for(int i=1;i+m-1<=n;i++){
//枚举所有长度为m的区间
int xx=sx[n]-sx[i+m-1]+sx[i-1];
int yy=sy[n]-sy[i+m-1]+sy[i-1];
//得到不需要改变的区间中,对x,y的数值所作出的贡献
int tx=x-xx;
int ty=y-yy;
//求出该需要改变的区间中,x和y想要到达终点,所需恰好作出的贡献
if(abs(tx)+abs(ty)<=m && (m-abs(tx)-abs(ty))%2==0) //(abs(tx)+abs(ty)位字符做出使该人刚好到达终点的贡献,剩下位的字符如果是偶数,那么就可以让其走的路程两两抵消,从而达到刚好到达终点的效果)
return true;
}
return false;
}
int main(){
scanf("%d",&n);
scanf("%s",s+1);//操作符
scanf("%d%d",&x,&y);///最终目标位置
sx[0]=0;sy[0]=0;
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));
}
//求出按照原始序列走,x和y的前缀和
int l=0,r=n;
int ans=-1;
while(l<=r){
//二分答案,枚举需要改变的最短区间长度
int mid=(l+r)>>1;
if(check(mid))ans=mid,r=mid-1;
//如果该区间长度符合,就继续二分,看更短的区间是否符合
else l=mid+1;
}
if(ans==-1)puts("-1");
else printf("%dn",ans);
return 0;
}
最后
以上就是淡然朋友为你收集整理的CF C Vasya and Robot的全部内容,希望文章能够帮你解决CF C Vasya and Robot所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复