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 ...
随机推荐
- css中font-size为0的妙用(消除内联元素间的间隔)
前言 <div> <input type="text"> <input type="button" value="提交& ...
- [再寄小读者之数学篇](2014-05-28 Ladyzhenskaya 不等式)
$$\bex f\in C_c^\infty(\bbR^2)\ra \sen{f}_{L^4}\leq \sqrt{2} \sen{f}_{L^2}^{1/2} \sen{\p_1f}_{L^2}^{ ...
- Nginx web 服务器 安装篇
Nginx介绍: 静态web服务器有Nginx .Apache .lighttpd等 目前国内用的最常见的就是Nginx 和Apache 是一个开源的.支持高性能.高并发的www服务和代理服务软件,N ...
- H5取经之路——添加hover实现特定效果
一.鼠标指上后显示二维码,效果图如下: 鼠标未指上时: 鼠标指上后: 代码如下: .div1 .li2 .code_wexin{ width: 0px; height: 0px; position: ...
- apache中 sed 指定文件中某字符串增加行
#!/bin/bash #在 servername 域名 字符串后面添加指定字符串 servername=`grep ServerName httpd-vhosts.conf |awk '{print ...
- java PDF2JPG
import org.apache.commons.lang3.StringUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org ...
- CentOS7上部署taiga项目管理软件
作者:waringid 一.简介 Taiga 是一个免费开源,而且功能非常强大的项目管理平台,用于初创企业和敏捷开发团队.提供一个简单.漂亮的项目管理工具.Taiga 采用 Python Django ...
- cf1154G 埃氏筛应用
直接用埃氏筛也可以做,但是这题写起来有点恶臭.. 更加简单的写法是直接枚举gcd=k,然后里面再枚举一次i*k,即找到k两个最小的倍数,看起来复杂度很高,但其实也是埃氏筛的复杂度 因为每次枚举gcd, ...
- 论文阅读笔记四十:Deformable ConvNets v2: More Deformable, Better Results(CVPR2018)
论文源址:https://arxiv.org/abs/1811.11168 摘要 可变形卷积的一个亮点是对于不同几何变化的物体具有适应性.但也存在一些问题,虽然相比传统的卷积网络,其神经网络的空间形状 ...
- 一篇图看清Java中的各种Queue
说到数据结构,我们大概可以列出这么几个:数组,链表,栈,队列,集合,哈希表. 其中 队列 作为一个常用的数据结构,在Java中也有各种形式的实现. 顶级接口为java.util.queue. java ...