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)(0,0). Robot can perform the following four kinds of operations:

  • U — move from (x,y)(x,y)(x,y) to (x,y+1)(x,y+1)(x,y+1);
  • D — move from (x,y)(x,y)(x,y) to (x,y−1)(x,y−1)(x,y−1);
  • L — move from (x,y)(x,y)(x,y) to (x−1,y)(x−1,y)(x−1,y);
  • R — move from (x,y)(x,y)(x,y) to (x+1,y)(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)(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+1maxID−minID+1, where maxIDmaxIDmaxID is the maximum index of a changed operation, and minIDminIDminID is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices 2,52, 52,5 and 777 are changed, so the length of changed subsegment is 7−2+1=67−2+1=67−2+1=6. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is 111.

If there are no changes, then the length of changed subsegment is 000. 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)(0,0) to (x,y)(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⋅10^5)n(1≤n≤2⋅105) — the number of operations.

The second line contains the sequence of operations — a string of nnn characters. Each character is either U, D, L or R.

The third line contains two integers x,y(−109≤x,y≤109)x,y (−10^9≤x,y≤10^9)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)(0,0) to (x,y)(x,y)(x,y). If this change is impossible, print −1−1−1.

Examples

input

5
RURUU
-2 3

output

3

input

4
RULR
1 1

output

0

input

3
UUU
100 100

output

-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=33−1+1=3.

In the second example the given sequence already leads the robot to (x,y)(x,y)(x,y), so the length of the changed subsegment is 000.

In the third example the robot can’t end his path in the cell (x,y)(x,y)(x,y).

题意

一个机器人从(0,0)(0,0)(0,0)点出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假设改变 字符串的最大下标为maxIDmaxIDmaxID ,改变字符串的最小下标为minIDminIDminID ,输出最小的 maxID−minID+1maxID-minID+1maxID−minID+1 ,即,输出最小的改变字符串的连续区间长度(该区间内的字符不一定要全部发生改变)

Solve

字符串长度小于原点到指定位置的距离,字符串长度与原点到指定位置具有不同的奇偶性。在这两种情况下,是无论如何都无法到达指定位置的。其余情况都一定有答案。

因为是要求区间的长度,所以二分枚举区间长度,对于每个区间长度尺取,找出所有可达的情况。如果某个区间长度可行,尝试去缩小当前区间长度;否则,延长区间长度

Code

/*************************************************************************
> File Name: C.cpp
> Author: WZY
> Created Time: 2019年02月15日 15:39:58
************************************************************************/ #include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
char ch[maxn];
int sumx[maxn],sumy[maxn];
int n;
int x,y;
// 判断区间是否可行
bool check(int len)
{
for(int l=1;l+len-1<=n;l++)
{
int r=l+len-1;
// 不需要改变的指令个数
int _x=sumx[l-1]+sumx[n]-sumx[r];
int _y=sumy[l-1]+sumy[n]-sumy[r];
// 计算当前点到指定点的距离
int sum=abs(_x-x)+abs(_y-y);
// 当前点到指定点的距离<=len并且多走的路程可以两两抵消
if(sum<=len&&(len-sum)%2==0)
return true;
}
return false;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>(ch+1);
cin>>x>>y;
for(int i=1;i<=n;i++)
{
if(ch[i]=='R')
sumx[i]=sumx[i-1]+1,sumy[i]=sumy[i-1];
else if(ch[i]=='L')
sumx[i]=sumx[i-1]-1,sumy[i]=sumy[i-1];
else if(ch[i]=='U')
sumx[i]=sumx[i-1],sumy[i]=sumy[i-1]+1;
else
sumx[i]=sumx[i-1],sumy[i]=sumy[i-1]-1;
}
int ans,l,r;
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;
}
cout<<ans<<endl;
return 0;
}

Codeforces 1073C:Vasya and Robot(二分)的更多相关文章

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

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

  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 二分

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

  4. C. Vasya and Robot二分

    1.题目描述 Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell ...

  5. codeforces 355C - Vasya and Robot

    因为在允许的情况下,必然是左右手交替进行,这样不会增加多余的无谓的能量. 然后根据不同的分界点,肯定会产生左手或右手重复使用的情况,这是就要加上Qr/Ql * 次数. 一开始的想法,很直接,枚举每个分 ...

  6. 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 ...

  7. Codeforces Round #115 A. Robot Bicorn Attack 暴力

    A. Robot Bicorn Attack Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/17 ...

  8. CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26

    /* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...

  9. 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, ...

随机推荐

  1. Mysql索引数据结构详解(1)

    慢查询解决:使用索引  索引是帮助Mysql高效获取数据的排好序的数据结构 常见的存储数据结构: 二叉树    二叉树不适合单边增长的数据 红黑树(又称二叉平衡树)    红黑树会自动平衡父节点两边的 ...

  2. day02 Linux基础

    day02 Linux基础 1.什么是服务器 服务器,也称伺服器,是提供计算服务的设备.由于服务器需要响应服务请求,并进行处理,因 此一般来说服务器应具备承担服务并且保障服务的能力. windows: ...

  3. 【leetocde】922. Sort Array By Parity II

    Given an array of integers nums, half of the integers in nums are odd, and the other half are even.  ...

  4. db9串口接头的定义

    这个接头都是以公头为准,所有接头还是以公头去记. RS-232端(DB9公头/针型)引脚定义 2: RXD 3:TXD 5:GND 1/4/6:内部相链接 7/8   :内部相链接 1.RS-232端 ...

  5. Android实现网络监听

    一.Android Wifi常用广播 网络开发中主体会使用到的action: ConnectivityManager.CONNECTIVITY_ACTION WifiManager.WIFI_STAT ...

  6. NSString类里有个hash

    实际编程总会涉及到比较两个字符串的内容,一般会用 [string1 isEqualsToString:string2] 来比较两个字符串是否一致.对于字符串的isEqualsToString方法,需要 ...

  7. 软件测试人员必备的linux命令

    1 目录与文件操作1.1 ls(初级)使用权限:所有人功能 : 显示指定工作目录下之内容(列出目前工作目录所含之档案及子目录). 参数 : -a 显示所有档案及目录 (ls内定将档案名或目录名称开头为 ...

  8. POST/GET请求中RequestBody和RequestParam的应用场景

    POST请求时 @RequestBody --> JSON字符串部分 @RequestParam --> 请求参数部分 application/json格局图   图一.png form- ...

  9. my36_InnoDB关键特性之change buffer

    一.关于IOT:索引组织表 表在存储的时候按照主键排序进行存储,同时在主键上建立一棵树,这样就形成了一个索引组织表,一个表的存储方式以索引的方式来组织存储的. 所以,MySQL表一定要加上主键,通过主 ...

  10. Java 使用slf4j记录日志

    引入依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12< ...