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


		 	 	 				   		  	 					   	 	

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-24 13:39
在记录秋招的大魔王很...:别被忽悠了,我做了多年销售。我可以告诉你,这就是忽悠你的,销售一定要看底薪也要看提成两者不可缺一。提成是有业绩的时候才拿的到的,谁能保证一直有单状态都好。销售有时候很讲究运气的。底薪是你这个人这个岗位日常工作体现的价值。别小看底薪,你看那些跳槽去做经理主管的,底薪底一些,人家愿意去吗?所以那些说销售靠提成的纯属忽悠,除非他们的业务很容易成单。
点赞 评论 收藏
分享
一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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