C. Vasya and Robot二分
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.
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.
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.
5
RURUU
-2 3
3
4
RULR
1 1
0
3
UUU
100 100
-1
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二分的更多相关文章
- Educational Codeforces Round 53 (Rated for Div. 2) C Vasya and Robot 二分
题目:题目链接 思路:对于x方向距离与y方向距离之和大于n的情况是肯定不能到达的,另外,如果n比abs(x) + abs(y)大,那么我们总可以用UD或者LR来抵消多余的大小,所以只要abs(x) + ...
- CF 1073C Vasya and Robot(二分答案)
C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 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 ...
- Codeforces 1073C:Vasya and Robot(二分)
C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...
- Codeforces 1073C Vasya and Robot 【二分】
<题目链接> 题目大意: 一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假 ...
- 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, ...
- 【CF1073C】Vasya and Robot(二分,构造)
题意:给定长为n的机器人行走路线,每个字符代表上下左右走,可以更改将一些字符改成另外三个字符,定义花费为更改的下标max-min+1, 问从(0,0)走到(X,Y)的最小花费,无解输出-1 n< ...
- 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 ...
- codeforces 676C C. Vasya and String(二分)
题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...
随机推荐
- line-height && vertical-align 学习总结
前言 line-height.font-size.vertical-align是设置行内元素布局的关键属性.这三个属性是相互依赖的关系,改变行间距离.设置垂直对齐等都需要它们的通力合作. 行高 lin ...
- [物理学与PDEs]第2章第4节 激波 4.2 熵条件
1. R.H. 条件仅仅给出了越过激波时的能量守恒定律, 即热力学第一定律; 但客观的流体运动过程还需满足热力学第二定律, 即越过激波是个熵增过程: $$\bex S_1>S_0\quad(0 ...
- windows下 cmd 界面的替代者 cmder 推荐!
介绍 http://cmder.net/ Portable console emulator for Windows Cmder is a software package created out o ...
- 第30月第6天 git log
1. git log git log 96a6f18b1e0a1b7301cb4f350537d947afeb22bc -p -1 我们常用 -p 选项展开显示每次提交的内容差异,用 -2 则仅显示最 ...
- ES6 的 一些语法
1,let 声明变量 let 声明的变量只能在let 的块级作用域中生效,也是为了弥补var声明变量的全局污染问题. var 声明变量有变量提升的作用,也就是在声明变量之前可以使用变量 console ...
- win10免安装版本的MySQL的下载安装和配置
下载mysql-xxx.zip(免安装版) 解压到自己想要的目录下(我的是D:\mysql\),打开mysql-5.7.21-winx64文件夹,新建my.ini文件,输入: [mysql] # 设置 ...
- 405 css样式的研究 list-style-type 属性研究
CSS 列表的样式 list-style-type.list-style-position和list-style-image 属性 在CSS中,列表元素是一个块框,列表中的每个表项也是一个块框,只是在 ...
- PageUtil.java分页工具类
package com.chabansheng.util; /** * 分页工具类 * @author Administrator * */ public class PageUtil { /** * ...
- 杨辉三角(用for循环)
public class Test413__________________ { public static void main(String[] args) { int rows = 10; for ...
- Misc杂项隐写题writeup
MISC-1 提示:if you want to find the flag, this hint may be useful: the text files within each zip cons ...