题目大意:

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

你需要取走一段区间

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

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

解题思路:

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

但是行走次数最大有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. spring事物(一),@EnableTransactionManagement @Transactional 启动解析

    1.事物的声明阶段 @EnableTransactionManagement,是我们开启注解事物的第一步,我们来看下这个类为我们干了什么 @Target(ElementType.TYPE) @Rete ...

  2. CentOS操作系统部署zabbix agent服务

    CentOS操作系统部署zabbix agent服务 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看zabbix的官方手册 1>.点击下载 2>.查看Ubunt ...

  3. Day7 - I - Semi-prime H-numbers POJ - 3292

    This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study th ...

  4. 03.swoole学习笔记--web服务器

    <?php //创建web服务器 $serv=); //获取请求 /* * $request:请求信息 * $response:响应信息 */ $serv->on('request',fu ...

  5. springmvc线程安全问题

    对于使用过SpringMVC和Struts2的人来说,大家都知道SpringMVC是基于方法的拦截,而Struts2是基于类的拦截.struct2为每一个请求都实例化一个action所以不存在线程安全 ...

  6. 数据结构——java Queue类

    定义 队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用 图例 Que ...

  7. 提升Windows系统舒适度软件

    1.Geek Uninstaller 卸载软件 2.PotPlayer 无广告播放器

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-remove

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  9. Web UI设计师需要了解的用栅格化系统指导网页设计

    出处:https://www.jianshu.com/p/9838f217f4f6 致敬,,, ---------------------------------------------------- ...

  10. P30 Pro劲敌!DxO官宣新机:排行榜又要变

    均为111分,接着是华为Mate 20 Pro.P20 Pro和三星Galaxy S10+,三款机型综合得分均为109分,前10名最后三款机型分别是小米9.iPhone XS Max和HTC U12+ ...