C2. Increasing Subsequence (hard version)

链接:https://codeforces.ml/contest/1157/problem/C2

The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).

You are given a sequence aa consisting of nn integers.

You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).

For example, for the sequence [1,2,4,3,2][1,2,4,3,2] the answer is 44 (you take 11 and the sequence becomes [2,4,3,2][2,4,3,2], then you take the rightmost element 22 and the sequence becomes [2,4,3][2,4,3], then you take 33 and the sequence becomes [2,4][2,4] and then you take 44 and the sequence becomes [2][2], the obtained increasing sequence is [1,2,3,4][1,2,3,4]).

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output

In the first line of the output print kk — the maximum number of elements in a strictly increasing sequence you can obtain.

In the second line print a string ss of length kk, where the jj-th character of this string sjsj should be 'L' if you take the leftmost element during the jj-th move and 'R' otherwise. If there are multiple answers, you can print any.

Examples

input

Copy

5
1 2 4 3 2

output

Copy

4
LRRR

input

Copy

7
1 3 5 6 5 4 2

output

Copy

6
LRLRRR

input

Copy

3
2 2 2

output

Copy

1
R

input

Copy

4
1 2 4 3

output

Copy

4
LLRR

Note

The first example is described in the problem statement.

 

代码

#include<bits/stdc++.h>
using namespace std;
long long t,n,m,k,s,l,r;
char x[200001];
long long a[200001];
int main()
{
	cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>a[i];
    }
    l=1,r=n;
    k=0;
    s=0;
    while(l<=r)
    {
    	if(a[l]<a[r])
    	{
    		if(a[l]>k)
    		{
    			k=a[l];
    			s++;
    			l++;
    			x[s]='L';
    		}
    		else if(a[r]>k)
    		{
    			k=a[r];
    			s++;
    			r--;
    			x[s]='R';
    		}
    		else
    		break;
    	}
    	else if(a[l]>a[r])
    	{
    		if(a[r]>k)
    		{
    			k=a[r];
    			s++;
    			r--;
    			x[s]='R';
    		}
			else if(a[l]>k)
    		{
    			k=a[l];
    			s++;
    			l++;
    			x[s]='L';
    		} 
    		else
    		break;
    	}
    	else
    	{
    		long long s1=0,s2=0,kk=k;
    		for(int i=l;i<=r;i++)
    		{
    			if(a[i]>k)
    			{
    				k=a[i];
    				//cout<<a[i]<<' ';
    				s1++;
    			}
    			else
    			break;
    		}
    		k=kk;
    		for(int i=r;i>=l;i--)
    		{
    			if(a[i]>k)
    			{
    				k=a[i];
    				//cout<<i<<" ";
    				s2++;
    			}
    			else
    			break;
    		}
    		//cout<<s1<<" "<<s2;
    		if(s1>s2)
    		{
    			for(int i=1;i<=s1;i++)
    			{
    				s++;
    				x[s]='L';
    			}
    		}
    		else
    		{
    			for(int i=1;i<=s2;i++)
    			{
    				s++;
    				x[s]='R';
    			}
    		}
    		break;
    	}
    }
    cout<<s<<endl;
	for(int i=1;i<=s;i++)
	{
		cout<<x[i];
	}
    
}


		 	 	 				   		  	 					   	 	

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
2025-12-17 16:48
今天九点半到公司,我跟往常一样先扫了眼电脑,屁活儿没有。寻思着没事干,就去蹲了个厕所,回来摸出手机刷了会儿。结果老板刚好路过,拍了我一下说上班别玩手机,我吓得赶紧揣兜里。也就过了四十分钟吧,我的直属领导把我叫到小隔间,上来就给我一句:“你玩手机这事儿把老板惹毛了,说白了,你可以重新找工作了,等下&nbsp;HR&nbsp;会来跟你谈。”&nbsp;我当时脑子直接宕机,一句话都没憋出来。后面&nbsp;HR&nbsp;找我谈话,直属领导也在旁边。HR&nbsp;说我这毛病不是一次两次了,属于屡教不改,不光上班玩手机,还用公司电脑看论文、弄学校的事儿。我当时人都傻了,上班摸鱼是不对,可我都是闲得发慌的时候才摸啊!而且玩手机这事儿,从来没人跟我说过后果这么严重,更没人告诉我在公司学个习也算犯错!连一次口头提醒都没有,哪儿来的屡教不改啊?更让我膈应的是,昨天部门刚开了会,说四个实习生里留一个转正,让大家好好表现。结果今天我就因为玩手机被开了。但搞笑的是,开会前直属领导就把我叫去小会议室,明明白白告诉我:“转正这事儿你就别想了,你的学历达不到我们部门要求,当初招你进来也没打算给你这个机会。”合着我没入贵厂的眼是吧?可我都已经被排除在转正名单外了,摸个鱼至于直接把我开了吗?真的太离谱了!
rush$0522:转正名单没进,大概率本来就没打算留你
摸鱼被leader发现了...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务