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. SpringBoot系列: SpringBoot Web项目中使用Shiro

    注意点有:1. 不要启用 spring-boot-devtools, 如果启用 devtools 后, 不管是热启动还是手工重启, devtools总是试图重新恢复之前的session数据, 很有可能 ...

  2. Linux文件权限命令及配置

    http://www.cnblogs.com/CgenJ/archive/2011/07/28/2119454.html

  3. library 显示所有的数据

    <?php  $conn = @mysql_connect('localhost', 'root', ''); if($conn) {  echo "连接成功"; }else ...

  4. Python 官方中文教程(简)

    Python 官方教程 前言 这是一次系统学习Python官方教程的学习笔记 整个教程一共16章, 在学习过程中记录自己不知道的和一些重要的知识, 水平有限, 请指正. Python3.7 官方教程. ...

  5. Android Studio项目Gradle内网配置

    由于内网无法连接到外部网络,在使用Gradle编译Android Studio项目时就会面临一些问题: 1.Gradle安装文件无法下载 2.Gradle Android插件无法下载 3.项目依赖文件 ...

  6. 加密:HashUtils,RSAUtil,AESUtils

    import java.security.MessageDigest; public class HashUtils { public static String getMD5(String sour ...

  7. 一 .isinstance(obj,cls)和issubclass(sub,super)

    class Foo: pass class Bar(Foo): pass obj = Bar() isinstance(obj,cls)检查是否obj是否是类 cls 的对象 print(isinst ...

  8. 期货大赛项目|十,MVC对js和css的压缩

    在Global.asax中添加两行代码 //默认在调试期间,不会启用js和css的压缩 //下面的语句确保了在调试期间也压缩css和js BundleTable.EnableOptimizations ...

  9. 手动部署 kubernetes HA 集群

    前言 关于kubernetes HA集群部署的方式有很多种(这里的HA指的是master apiserver的高可用),比如通过keepalived vip漂移的方式.haproxy/nginx负载均 ...

  10. hdu4791-Alice's Print Service

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 题目解释:给你一组数据s1,p1,s2,p2...sn,pn,一个数字q,问你当要打印q张资料时 ...