概述
题目链接
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长度的操作序列,求出在这序列中最小的需要改变的区间长度,能使它从(0,0)到(x,y)。
【解题思路】
反正我是想不到二分qaq
先对x,y都做一下预处理求前缀和,即原始指令字符串对x,y的改变所作出的贡献。二分区间长度,并检查这个长度是否能够满足题意,利用前缀和计算出理论上该区间x,y恰好所需的改变,然后怎样算满足题意呢?首先需要理论上所需的贡献值小于等于这个区间的长度,其次,区间t=长度-理论值应该是个偶数,因为t显然是多余的,所以剩下位的字符如果是偶数,那么就可以让其走的路程两两抵消,从而达到刚好到达终点的效果。
【代码】
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
char s[maxn];
int sumx[maxn],sumy[maxn];
int ex,ey,sx=0,sy=0,n;
bool check(int len)
{
for(int i=1;i+len-1<=n;i++)
{
int j=i+len-1;
int curx=sumx[i-1]+sumx[n]-sumx[j];
int cury=sumy[i-1]+sumy[n]-sumy[j];
//去除len区间后,x,y所做的贡献
int delta=abs(ex-curx)+abs(ey-cury);
//得到len区间中,x,y到达终点所需要的贡献
if(delta<=len && (len-delta)%2==0)return true;
}
return false;
}
int main()
{
scanf("%d",&n);
scanf("%s",s+1);
scanf("%d%d",&ex,&ey);
for(int i=1;i<=n;i++)
{
sumx[i]=sumx[i-1];sumy[i]=sumy[i-1];
if(s[i]=='L')sumx[i]-=1;
else if(s[i]=='R')sumx[i]+=1;
else if(s[i]=='D')sumy[i]-=1;
else sumy[i]+=1;
}
int l=0,r=n,ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%dn",ans);
}
最后
以上就是懦弱金鱼为你收集整理的【Codeforces1073C】Vasya and Robot(二分+思维+前缀和)的全部内容,希望文章能够帮你解决【Codeforces1073C】Vasya and Robot(二分+思维+前缀和)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复