Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)

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 a

consisting of n

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]

the answer is 4 (you take 1 and the sequence becomes [2,4,3,2], then you take the rightmost element 2 and the sequence becomes [2,4,3], then you take 3 and the sequence becomes [2,4] and then you take 4 and the sequence becomes [2], the obtained increasing sequence is [1,2,3,4]

).

Input

The first line of the input contains one integer n

(1≤n≤2⋅105) — the number of elements in a

.

The second line of the input contains n

integers a1,a2,…,an (1≤ai≤2⋅105), where ai is the i-th element of a

.

Output

In the first line of the output print k

— the maximum number of elements in a strictly increasing sequence you can obtain.

In the second line print a string s

of length k, where the j-th character of this string sj should be 'L' if you take the leftmost element during the j

-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 <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define ll long long
#include<stack>
#include<set>
int a[220000];
char b[220000];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(b,0,sizeof(b));
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        int x=0,y=n+1,sum=0,t=0;
        while(x<y)
        {
            int x1,y1;
            x1=x+1;
            y1=y-1;
            if(x1==y1)
            {
                if(a[y1]>sum)
                {
                    y=y1;
                    x=x1;
                    b[t++]='R';
                }
                break;
            }
            if(a[x1]<=sum&&a[y1]<=sum)
                break;
            if(a[x1]>sum&&a[y1]>sum)
            {
                if(a[x1]>a[y1])
                {
                    sum=a[y1];
                    y=y1;
                    b[t++]='R';
                    continue;
                }
                else if(a[x1]==a[y1])
                {

                    int x2,y2,t1=1,t2=1;
                    for(int j=x1;j<=y1-1;j++)
                    {
                        if(a[j+1]>a[j]) t1++;
                        else break;
                    }
                    for(int j=y1;j>=x1+1;j--)
                    {
                        if(a[j-1]>a[j]) t2++;
                        else break;
                    }
                    if(t1>t2)
                    {
                        x=x1+t1-1;
                        y=y1-t1+1;
                        sum=a[x1+t1-1];
                        for(int i=1;i<=t1;i++) b[t++]='L';
                    }
                    else
                    {
                        y=y1-t2+1;
                        x=x1+t2-1;
                        sum=a[y1-t2+1];
                        for(int i=1;i<=t2;i++) b[t++]='R';
                    }
                    //printf("t1=%d t2=%d x=%d y=%d\n",t1,t2,x,y);
                    break;
                }
                else
                {
                    sum=a[x1];
                    x=x1;
                    b[t++]='L';
                    continue;
                }
            }
            else if(a[x1]>sum)
            {
                sum=a[x1];
                x=x1;
                b[t++]='L';
                continue;
            }
            else
            {
                sum=a[y1];
                y=y1;
                b[t++]='R';
                continue;
            }
        }
        printf("%d\n%s\n",t,b);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值