题目大意:

给定一个机器人的行走方式

你需要取走一段区间

但要保证取走这段区间后机器人最终到达的终点位置是不变的

问这段区间最短时是哪一段

解题思路:

易得,如果重复走到了某些已经走过的点,那么肯定就有一段区间可以被删除

但是行走次数最大有2e5,即用数组记录坐标状态的话起码要开4e5*4e5的空间,显然不可能

所以可以用map储存上一次走到某个坐标是第几步

那么每次只要判断当前的坐标是否已经被走过即可,走过的话就尝试更新答案

因为map中未调用过的int值为0

所以让原点的步数设置为1防止混淆

初始设置 l=0,r=n+1去最大化这个答案区间,便于第一次判断得以执行

然后,遍历这个字符串,如果发现某个点已经走过了,取出这个步数为 d

可以得到 d+1~i 这一段是可以删除的(d步不可取,因为那一步走完才能到达这个点)

那么就拿 i-(d+1) 和 r-l 作比较,即当 r-l>i-(d+1)  =>  r-l>=i-d 时,更新lr答案

最后把第一步的步数减回去就可以作为答案了(l=0,说明不存在)

对于这里的结构体运算符重载,因为使用map需要告知大小关系才能存入这颗红黑树进行排序(map是会根据键值进行大小排序先的,然后进行二分查找)

所以就重载了小于运算符,先按照x从小到大排,如果x相同按照y从小到大排

这样就可以在之后二分查找时能够准确找到值

然后是对等于的重载,在查找的最后需要判断找到的位置的键值和输入的是否相同(因为如果找不到输入的键值的话是会返回空的——int类型即数值0)

所以要重载等于运算符

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct P{
int x,y;
bool operator < (const P& a) const{
return x<a.x||x==a.x&&y<a.y;
}
bool operator == (const P& a) const{
return x==a.x&&y==a.y;
}
};
string s;
void solve(){
int i,n,l,r,d;
cin>>n>>s;
map<P,int> mp;
P pd;
pd.x=pd.y=;
mp[pd]=;
l=;r=n+;//先最大化答案区间
s=" "+s;//下标向右移动2位,便于直接把i当作步数
for(i=,n++;i<=n;i++){
if(s[i]=='L')
pd.x--;
else if(s[i]=='R')
pd.x++;
else if(s[i]=='U')
pd.y++;
else
pd.y--;
d=mp[pd];
if(d>){//这个点走过
if(r-l>=i-d)
l=d+,r=i;
}
mp[pd]=i;
}
if(l==)
cout<<"-1\n";
else
cout<<l-<<' '<<r-<<'\n';//因为原点步数为1,把差值减回去
}
int main(){
ios::sync_with_stdio();
cin.tie();cout.tie();
int T;cin>>T;while(T--)
solve(); return ;
}

Codeforces 1296C - Yet Another Walking Robot的更多相关文章

  1. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

  2. leetcode 874. Walking Robot Simulation

    874. Walking Robot Simulation https://www.cnblogs.com/grandyang/p/10800993.html 每走一步(不是没走commands里的一 ...

  3. 【Leetcode_easy】874. Walking Robot Simulation

    problem 874. Walking Robot Simulation solution1: 思路:1)如何表示移动的方向以及移动的位置坐标; 2)障碍物坐标如何检查;3)求解的是最大距离; cl ...

  4. C. Yet Another Walking Robot Round #617 (Div. 3)()(map + 前后相同状态的存储)

    C. Yet Another Walking Robot time limit per test 1 second memory limit per test 256 megabytes input ...

  5. Codeforces 1154D - Walking Robot - [贪心]

    题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...

  6. Codeforces #617 (Div. 3) C. Yet Another Walking Robot

    There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0)(0,0) . It ...

  7. Codeforces 752C - Santa Claus and Robot - [简单思维题]

    题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...

  8. [Swift]LeetCode874. 模拟行走机器人 | Walking Robot Simulation

    A robot on an infinite grid starts at point (0, 0) and faces north.  The robot can receive one of th ...

  9. Codeforces 294D - Shaass and Painter Robot

    294D - Shaass and Painter Robot 思路: 可以用数学归纳法证明一个结论:整个棋盘黑白相间当且仅当边缘黑白相间. 分奇偶讨论又可得出边缘黑色格个数为n+m-2 这样就可以暴 ...

随机推荐

  1. upload-labs-env文件上传漏洞 11-19关

    Pass-11 源码:加上了本人的注释=.= $is_upload = false; $msg = null; if(isset($_POST['submit'])){ $ext_arr = arra ...

  2. P1080 MOOC期终成绩

    1080 MOOC期终成绩 (25分)   对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分 ...

  3. 在GNOME开发人员的努力下,Pango 1.44即将问世

    早在5月份,Red Hat的Matthias Clasen共同制定了计划,在近年来相当陈旧的情况下,对Pango布局引擎库进行了一些改进. 这项工作将随着Pango1.44版本的发布而实现,看起来它很 ...

  4. 赶在EW2020之前,FreeRTOS发布V10.3.0,将推出首个LTS版本

    点击下载:FreeRTOSv10.3.0.exe 说明: 1.新版更新: (1)对于IAR For RISC-V进行支持,并且加强了对RISC-V内核芯片支持,做了多处修正. (2)对阿里平头哥CH2 ...

  5. windows搭建安装react-native环境

    在win10环境下,利用Genymotion模拟器,配置react-native的环境. 一.安装JDK 在网上下载jdk,版本最好是1.8以上.安装后要对环境变量进行配置. 同时在 Path 中配置 ...

  6. 标准C的标记化结构初始化语法

    1 struct file_operations { 2         struct module *owner; 3         loff_t (*llseek) (struct file * ...

  7. NO8 find结合sed查找替换企业案例多方法精讲&命令总结!

    ·sed    #替换  eg: sed 'sed 's#已有的内容#更改的内容#g' oldboy.txt                   s 代表替换,g代表全局,sg就是全局替换       ...

  8. Django 项目搭建

    django(mvt结构) 虚拟环境 创建虚拟环境 mkvirtualenv django_py3 -p python3 切换虚拟环境 wokeon 虚拟环境名称 删除虚拟环境 rmvirtualen ...

  9. Spring入门之四-------SpringIoC之其他知识点

    一.懒加载 public class Bean1 { public Bean1() { System.out.println(this.getClass().getSimpleName() + &qu ...

  10. 操作数据库的SqlHelper类

    public static class SqlHelper { public static readonly string connstr = ConfigurationManager.Connect ...