第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);
  }
}


总结:


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


目录
相关文章
|
机器学习/深度学习 人工智能 BI
第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南),签到题5题
第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南),签到题5题
70 0
|
5月前
|
人工智能 NoSQL 机器人
第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京),签到题4题
第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京),签到题4题
105 0
|
11月前
|
人工智能 BI
第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南),签到题2题
第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南),签到题2题
73 2
|
5月前
|
机器学习/深度学习 人工智能
第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明),签到题4题
第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明),签到题4题
69 0
|
人工智能 移动开发 分布式计算
第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京),签到题5题
第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京),签到题5题
217 0
|
机器学习/深度学习 物联网 BI
第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明),签到题3题
第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明),签到题3题
141 0
第14届蓝桥杯模拟赛 第2期
请找到一个大于 2022 的最小数,这个数转换成二进制之后,最低的 6 个二进制为全为 0 。请将这个数的十进制形式作为答案提交。
|
机器学习/深度学习 Java 定位技术
第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(沈阳),签到题5题
第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(沈阳),签到题5题
292 0
|
机器学习/深度学习 人工智能 算法
2021-2022年度第三届全国大学生算法设计与编程挑战赛(秋季赛)G.希望(组合数学 bfs)
2021-2022年度第三届全国大学生算法设计与编程挑战赛(秋季赛)G.希望(组合数学 bfs)
238 0
2021-2022年度第三届全国大学生算法设计与编程挑战赛(秋季赛)G.希望(组合数学 bfs)
|
算法
2021-2022年度第三届全国大学生算法设计与编程挑战赛(秋季赛)热身赛 B.这是一道大水题(树状数组)
2021-2022年度第三届全国大学生算法设计与编程挑战赛(秋季赛)热身赛 B.这是一道大水题(树状数组)
158 0
2021-2022年度第三届全国大学生算法设计与编程挑战赛(秋季赛)热身赛 B.这是一道大水题(树状数组)