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

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天都想发疯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值