1.题目描述

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

2.思路:

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

先按照题目中给的路径计算x,y移动的位置,再二分判断修改的地方在哪里。

代码:

 #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 = ;
char s[SIZE];
int ex,ey;
int xSum[SIZE];
int ySum[SIZE];
int dx[],dy[];
dy['U'] = ;
dy['D'] = -;
dx['L'] = -;
dx['R'] = ;
//判断下一步在哪个区间段内行走
bool judge(int n, int len, int ex,int ey){
for(int i = ; i + len- <= n; ++i){
int curx = xSum[i-] + xSum[n]-xSum[i+len-];
// 去除长度 len长度 后的 x 走到的位置
int cury = ySum[i-] + ySum[n]-ySum[i+len-];
// 去除长度 len 长度后的 y走到的位置
int delta = abs(curx-ex) + abs(cury-ey);
// 距离到达终点还需要多少步
if(delta <= len && (len-delta)% == ) // 到达终点还需要的步数 一定小于 目前可以通过改变方向的那些步数的个数 len
return true; // 并且 因为此时 len两端 必须是改变的(0 或者 1 是特殊情况) len与delta差值 必须为偶数才能到终点
}
return false;
} int main()
{
int n;
scanf("%d\n%s",&n,s+);
scanf("%d%d",&ex,&ey);
//先算出题目给定路径的最后位置
for(int i = ; i <= n; ++i){
xSum[i] = xSum[i-] + dx[s[i]];
ySum[i] = ySum[i-] + dy[s[i]];
}
int lb = ,ub = n;
int ans = -,mid;
while(lb <= ub){
mid = (lb+ub)/;
if(judge(n,mid,ex,ey)){
ans = mid;
ub = mid-;
}
else{
lb = mid+;
}
}
printf("%d\n",ans);
return ;
}

C. Vasya and Robot二分的更多相关文章

  1. Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分

    题目:题目链接 思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + ...

  2. CF 1073C Vasya and Robot(二分答案)

    C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  4. Codeforces 1073C:Vasya and Robot(二分)

    C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...

  5. Codeforces 1073C Vasya and Robot 【二分】

    <题目链接> 题目大意: 一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假 ...

  6. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分或者尺取)

    题目哦 题意:给出一个序列,序列有四个字母组成,U:y+1,D:y-1 , L:x-1 , R:x+1;   这是规则 . 给出(x,y) 问可不可以经过最小的变化这个序列可以由(0,0) 变到(x, ...

  7. 【CF1073C】Vasya and Robot(二分,构造)

    题意:给定长为n的机器人行走路线,每个字符代表上下左右走,可以更改将一些字符改成另外三个字符,定义花费为更改的下标max-min+1, 问从(0,0)走到(X,Y)的最小花费,无解输出-1 n< ...

  8. Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. codeforces 676C C. Vasya and String(二分)

    题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...

随机推荐

  1. ElasticSearch评分分析 explian 解释和一些查询理解

    ElasticSearch评分分析 explian 解释和一些查询理解 按照es-ik分析器安装了ik分词器.创建索引:PUT /index_ik_test.索引包含2个字段:content和nick ...

  2. C#使用ServiceStack读写Redis

    通过C#第三方库向Redis存储数据遇到的几个问题 https://github.com/ServiceStack/ServiceStack.Redis 1.将对象转json字符串 JsonObjec ...

  3. [物理学与PDEs]第3章第4节 磁流体力学方程组的数学结构

    1.  在流体存在粘性.热传导及 $\sigma\neq \infty$ 时, 磁流体力学方程组是一个拟线性对称双曲 - 抛物耦合组. 2.  在流体存在粘性.热传导但 $\sigma=\infty$ ...

  4. 使用IntelliJ IDEA 配置Maven(转)

    1. 下载Maven 官方地址:http://maven.apache.org/download.cgi 解压并新建一个本地仓库文件夹 2.配置本地仓库路径 3.配置maven环境变量 4.在Inte ...

  5. MYSQL(三)

    转载自https://www.cnblogs.com/wupeiqi/articles/5716963.html 1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数 ...

  6. Saltstack自动化操作记录(2)-配置使用

    之前梳理了Saltstack自动化操作记录(1)-环境部署,下面说说saltstack配置及模块使用: 为了试验效果,再追加一台被控制端minion机器192.168.1.118需要在master控制 ...

  7. 在visual studio 2013中编译Lua5.3.1

    注:以下是基于 别人的教程或笔记来操作并按照自己的操作记录的纯文字版编译和hello lua过程. 原图文版链接: 原文链接 1.创建空的解决方案: 文件->新建->项目->其他项目 ...

  8. Solidity属性和方法的访问权限

    属性:默认是internal的类型,外部是不可以访问调用的,如果加上public的话,那么是会自动为这个属性加上一个get的方法的,比如uint   public _age; => functi ...

  9. $a=[1,2,3,4,5]; $b=[a,b,c,d,e]; 转成[[1,a],[2,b],[3,c],[4,d],[5,3]]

    $a=[1,2,3,4,5]; $b=[a,b,c,d,e]; 结果 [[1,a],[2,b],[3,c],[4,d],[5,3]] return array_map(function($v1,$v2 ...

  10. Confluence 使用常见问题列表

    Confluence 6 管理 Atlassian 提供的 App 摘要: Confluence 用户可以使用桌面应用来编辑一个已经上传到 Confluence 的文件,然后这个文件自动保存回 Con ...