任意门:http://codeforces.com/contest/1073/problem/C

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

题意概括:

输入N个操作,询问修操作是否能到达终点,如果能到达终点输出“修改的区间”;

修改区间的定义:修改的最大坐标操作的坐标 - 修改的最小坐标操作的坐标

【规定起点为 (0,0),输入终点( x, y );】

操作(上下左右):

    • 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)

解题思路:二分 || 尺取

预处理路径前缀和(压缩路径)sum_x [ ],sum_y[ ] ;则sum_x[ N ] , sum_y[ N ] 为实际的终点;

输入的终点为 (ex, ey),假设能修改若干个操作到达输入的终点,那么:

某一段 [ st, ed ] 所走的影响为:

              X轴方向:xx = ex - ( sum_x[ N ] - sum_x[ ed -1 ] + sum_x[ st - 1 ] )

              Y轴方向:yy = ey - ( sum_y[ N ] - sum_y[ ed - 1 ] + sum_y[ st - 1 ] )

二分

二分修改区间长度 len ,尺取判断在该长度是否满足修改条件;

①操作所走最大范围不得超过 len ,因为每次操作只是上下左右移动一步

②判断能否完成假设的影响  len - abs(xx) - abs(yy))%2 ?= 0;

 abs(xx)表示的是 x 方向 到达终点 ex 的差值

 abs(yy)表示的是 y 方向 到达终点 ey 的差值

 假如 len > abs(xx)+abs(yy) 说明这段区间有操作是多余的,但是只要剩下的操作数是偶数就可以两两抵消。

尺取:

直接定义一个头指针一个尾指针,暴力一遍,条件判断是要头指针加还是尾指针加,记录最小修改区间。

AC code:

 #include<bits/stdc++.h>
using namespace std;
const int N=1e6+;
int x[N],y[N];
int sx,sy,n;
char s[N];
bool check(int m)
{
for(int i=;i<=n-m+;i++)
{
int tx=x[n]-x[i+m-]+x[i-]; //当前原来选项造成的横坐标影响
int ty=y[n]-y[i+m-]+y[i-]; //当前原来选项造成的纵坐标影响
int ex=sx-tx, ey=sy-ty; //消除当前影响
printf("%d %d m: %d\n",ex,ey,m);
if(m>=(abs(ex)+abs(ey)) && (m-abs(ex)-abs(ey))%==) //可以构造
return ;
}
return ;
}
int main()
{
//string s;
while(~scanf("%d",&n))
{
scanf("%s",s+);
x[]=y[]=;
for(int i=;i<=n;i++)
{
x[i]=x[i-];y[i]=y[i-]; //累积前面步数的结果 if(s[i]=='L') x[i]-=; //当前步数造成的影响
else if(s[i]=='R') x[i]+=;
else if(s[i]=='D') y[i]-=;
else y[i]+=;
}
// printf("%d %d\n",x[n],y[n]);
scanf("%d %d",&sx,&sy); //终点
// printf("HH");
int l=,r=n,ans=-;
while(l<=r) //二分答案
{
printf("l:%d r:%d\n", l, r);
int mid=(l+r)>>;
if(check(mid)) ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}
}

最新文章

  1. NSDateFormatter 时间格式转换
  2. mysql系统数据库
  3. PHP中的位运算与位移运算(其它语言通用)
  4. Intellij IDEA 工具快捷键
  5. JAVA中继承时方法的重载(overload)与重写/覆写(override)
  6. 二模08day2解题报告
  7. lintcode:两个数的和
  8. rabbitMQ 笔记
  9. Spring声明式事务(xml配置事务方式)
  10. Boost::Thread 多线程的基础知识
  11. HDU 2073 无限的路
  12. Python thread local
  13. 即将上线的Imcash是何方神圣?
  14. 在form表单里上传图片
  15. 编译Hadoop 2.7.2支持压缩 转
  16. [UWP 自定义控件]了解模板化控件(2.1):理解ContentControl
  17. PHP遍历一个文件夹下所有文件和子文件夹的函数
  18. Arduino入门笔记(4):用蜂鸣器演奏音乐并配有LED闪烁
  19. 【Beta阶段】第五次Scrum Meeting!
  20. django views视图

热门文章

  1. mongodb的增删改查
  2. cloudermanager安装时database connection出现Unexpected error. Unable to verify database connection(图文详解)
  3. pulic——功能性(自己写完测试的)
  4. unity向量计算
  5. git win7 dos下设置代理
  6. Windows x64位通过PEB获得Kernel32基地址
  7. Javascript模块化编程(二)AMD规范(规范使用模块)
  8. 嵌入式Tomcat Web服务器的使用
  9. mysql主从复制报错 :Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
  10. Oracle 查询当前系统时间十分钟之前的记录,时间比较SQL