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];
	}
    
}


		 	 	 				   		  	 					   	 	

 

全部评论

相关推荐

牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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