CF1324C Frog Jumps solution to a problem

Original title link

Briefly meaning of the questions:

Now river have \ (n + 2 \) stones, ID \ (0 \) to the \ (n-+. 1 \) , \ (. 1 \) ~ \ (n-\) stones each block has a direction, if is \ (L \) then, the stone then only the frog to jump to the left, if it is \ (R & lt \) only to the right, of course, the \ (0 \) direction of the block of stone is \ (R & lt \ ) .

Now the frog from \ (0 \) jumps \ (the n-+ 1 \) , he should ask how to make him jump jump jump during the longest minimum distance of it? The output of this distance.

Obviously, we consider greedy.

C title is not said than done

You think if you can either jump \ (L \) point, they can jump \ (R \) point, which would you choose it?
Of course \ (R \) point of it!

How to explain it? (Rigorous point)

If \ (L \) in \ (R \) to the left, then jump \ (L \) after, can only have been left until a jump \ (R \) up (can not jump back). Well, you jump at this number \ (L \) in the first \ (L \) time, given that they jump \ (L \) , then you directly choose that \ (R \) is not on the list, but also waste so much step.

If \ (L \) in \ (R \) to the right, then jump \ (L \) After that, you can only return to the \ (R \) point; otherwise you can only have jumped back until another \ (R \) ; then you skip to the path again you jump from the back, I had to jump back, then what is the point, but also a waste of so many steps.

Therefore, the above description has been that we do not jump \ (L \) , just jump \ (R \) is optimal. (Minimum number of steps)

So, the minimum distance of the longest jump much is it? Not that the two adjacent \ (R \) maximum distance of it?

Analyze so much, finally found the nature of the topic, you can directly solve the problem!

Time complexity: \ (O (T \ n-Times) \) .

Because no longer than \ (10 ^ 5 \) , it is \ (O (10 ^ 5) \) .

Space complexity: \ (O (n-) \) .

Actual score: \ (100 pts \) .

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

const int N=2e5+1;

inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
	int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}

int T,n;
char s[N];

int main(){
	T=read(); while(T--) {
		scanf("%s",s);
		n=strlen(s);
		int k=-1,maxi=0; //k是上一个 R 的位置。因为 s 数组从 0 开始,所以这里从 -1 开始
		for(int i=0;i<n;i++)
			if(s[i]=='R') {
				maxi=max(i-k,maxi); //与上一个 R 的位置的距离
				k=i; //更新 R 的位置
			} 
		printf("%d\n",max(maxi,n-k));	//最后一个 R 到终点的距离也算一个答案
	}
	return 0;
}

Guess you like

Origin www.cnblogs.com/bifanwen/p/12550138.html