第10届山东省赛Wandering Robot(详细思路)

简介: 第10届山东省赛Wandering Robot(详细思路)

描述:


DreamGrid creates a programmable robot to explore an infifinite two-dimension plane. The robot has a basic instruction sequence a1,a2,…,an and a “repeating parameter” k, which together form the full instruction sequence s1,s2,…,sn,sn+1,…,snk and control the robot.


There are 4 types of valid instructions in total, which are ‘U’ (up), ‘D’ (down), ‘L’ (left) and ‘R’ (right).


Assuming that the robot is currently at (x, y), the instructions control the robot in the way below:


U: Moves the robot to (x,y+1).

D: Moves the robot to (x,y−1).

L: Moves the robot to (x−1,y).

R: Moves the robot to (x+1,y).

The full instruction sequence can be derived from the following equations


The robot is initially at (0, 0) and executes the instructions in the full instruction sequence one by one. To estimate the exploration procedure, DreamGrid would like to calculate the largest Manhattan distance between the robot and the start point (0, 0) during the execution of the nk instructions.


Recall that the Manhattan distance between (x1,y1) and (x2,y2) is defifined as |x1−x2|+|y1−y2|.


输入:


There are multiple test cases. The first line of the input contains an integer T indicating the number of test cases. For each test case:


The first line contains two integers n and k (1≤n≤105, 1≤k≤109), indicating the length of the basic instruction sequence and the repeating parameter.


The second line contains a string A=a1a2…an (|A|=n, ai∈{‘L′,‘R′,‘U′,‘D′}), where ai indicates the i-th instruction in the basic instriction sequence.


It’s guaranteed that the sum of |A| of all test cases will not exceed 2×106.


输出;


For each test case output one line containing one integer indicating the answer.


大意:


给出一个长度为n的方向序列,重复走这个方向序列 k 次,求 这 k 次最大的曼哈顿距离


思路:最大的曼哈顿距离只会出现在第一次重复或最后一次重复中,这题的坑点就在于容易忽略最大曼哈顿距离出现在第一次重复的情况;


我这题一开始就掉到坑里去了,搜了很多题解也没给出最大曼哈顿距离在第一次重复的情况的例子,问了队友才明白,记录一下;


1.每一次重复走方向序列对于横向和竖向的贡献是一定的,我们方便理解以横向为例子,加假如方向序列是 RRRLLLL 那么每走一次对与横向的贡献就是一个L,当重复的次数K比较小的时候,就会出现最大曼哈顿距离出现在第一次的情况,例如RRRLLLL重复 k=2 次,方向总体趋势是向左走的,但因为重复的次数太少,向左最远才走到 -2 的位置,但一次循环中向右走到过3的位置,所以最大出现在第一次循环中;

2.对于RRRLLLL这种情况,当 k 非常大的时候,最后的点无限靠近负无穷,那么最大的曼哈顿距离就一定出现在最后一次循环中,中间的循环过程加上贡献即可,最大曼哈顿距离出现在最后一次循环中;


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll maxx = 1e18;
const int N = 1e6+100;
const int p = 1e4;
const double eps = 1e-8;
int t;
ll n,k,xx,yy,maxlen;
string s;
int main()
{
  cin>>t;
  for(int i=1;i<=t;i++)
  {
    scanf("%lld %lld",&n,&k);
    cin>>s;
    xx=0;
    yy=0;
    maxlen=0;
    for(int j=0;j<n;j++)
    {
      if(s[j]=='U') yy++;
      if(s[j]=='D') yy--;
      if(s[j]=='L') xx--;
      if(s[j]=='R') xx++; 
      maxlen=max(maxlen,abs(xx)+abs(yy));
    }//第一次循环
    xx*=(k-1);
    yy*=(k-1);//中间的加上贡献
    for(int j=0;j<n;j++)
    {
      if(s[j]=='U') yy++;
      if(s[j]=='D') yy--;
      if(s[j]=='L') xx--;
      if(s[j]=='R') xx++; 
      maxlen=max(maxlen,abs(xx)+abs(yy));
    }//最后一次循环
    printf("%lld\n",maxlen);
  }
}


总结:


当每次循环的贡献非常小循环次数又非常少的时候,最后的总贡献会小于第一次循环中的最大曼哈顿距离,注意这种情况,下次更加细心!


目录
相关文章
|
机器学习/深度学习 监控 数据可视化
SIGGRAPH 2023论文奖公布,山大、港大获奖,北大、腾讯光子获提名
SIGGRAPH 2023论文奖公布,山大、港大获奖,北大、腾讯光子获提名
267 0
|
资源调度 算法 机器人
北工大校友Cheng Zhang获SIGGRAPH最佳博士论文奖
北工大校友Cheng Zhang获SIGGRAPH最佳博士论文奖
148 0
第14届蓝桥杯模拟赛 第2期
请找到一个大于 2022 的最小数,这个数转换成二进制之后,最低的 6 个二进制为全为 0 。请将这个数的十进制形式作为答案提交。
|
芯片 异构计算
【数字设计】芯动科技|芯原科技_2023届_笔试面试题目分享
【数字设计】芯动科技|芯原科技_2023届_笔试面试题目分享
【数字设计】芯动科技|芯原科技_2023届_笔试面试题目分享
|
C语言 C++
《C游记》 第一章 - 灵根孕育源流出 初识C言大道生(贰)
《C游记》 第一章 - 灵根孕育源流出 初识C言大道生(贰)
210 0
|
IDE Java 编译器
《C游记》 第一章 - 灵根孕育源流出 初识C言大道生(壹)
《C游记》 第一章 - 灵根孕育源流出 初识C言大道生(壹)
182 0
|
机器人
CMU大学生真的强上天了:设计登月车,2021年代表美国登月
人家卡耐基·梅隆的本科生,搞的这个东西真的exciting:他们做一个项目设计出来的漫游车,即将在2021年被NASA发射上月球,成为美国第一个探索月面的登月车。
258 0
CMU大学生真的强上天了:设计登月车,2021年代表美国登月
|
机器学习/深度学习 人工智能 自然语言处理
天池读书会三月场,邱锡鹏教授等一众大咖和你一起读书
阿里云天池读书会三月场来啦,这次我们邀请到了《零基础学机器学习》作者黄佳老师、蒲公英书《神经网络与深度学习》作者邱锡鹏教授、《数据分析通识》作者途索老师、《人工智能简史(第二版)》作者尼克老师、南瓜书《机器学习公式详解》作者谢文睿 、秦州(按直播分享时间排序)为大家进行精彩的图书分享。
1000 0
天池读书会三月场,邱锡鹏教授等一众大咖和你一起读书