Increasing Subsequence (hard version) CodeForces - 1157C2

18 篇文章 0 订阅
8 篇文章 0 订阅

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

 

题意:很简单直观,就是不断从左或右边取一个数按照顺序组成一个序列,必须要组成一个严格递增的序列。最后将从左右取数的顺序输出出来,用LR表示左右。

 

分析:题意很直观,但具体何种情况下取什么数有点复杂,这里要分析清楚,什么时候取左边的,什么时候取右边的?

我们分类讨论下整个题目会出现的情况,然后分别制定相应的取数策略:

1. 左右两个数都比我们上次选择的数字大,那么就选左右两个数中偏小的数

2. 左右两个数字相等但都比上次所取的数字大,那么我们要选择那个严格递增序列较长的那一边,不难发现,这样一选,只能一条路走到黑,因为一旦选择就会一直选择下去,那么另外一边就不会选择了,我们要实现的是严格递增序列。

3. 左右两个数一边大于上次选择的数,一边小于上次选的数,那么下一个选择较大的那个数。

4. 左右两个数都比上次选的小,那么直接就不选了。

题意理解不难,主要是分类讨论要想清楚。

 

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 2e5+10;
int a[maxn];
char ans[maxn];
int top;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    /*
        1.两边都较大 选其中较小的
        2.两边一样大 选其中递增序列长的
        3.一边较大 选择较大的
        4.两边都不较大 不选择
    */
    int l=1,r=n,flag = 0;
    while(1){
        if(l>r)break;
        if(l>n||r<1)break;
        if(a[l]!=a[r]&&a[l]>flag&&a[r]>flag)
        {
            if(a[l]<a[r])
            {
                ans[top++] = 'L';
                flag = a[l];
                l++;
            }
            else
            {
                ans[top++] = 'R';
                flag = a[r];
                r--;
            }
        }
        else if(a[l]==a[r]&&a[l]>flag)
        {
            int lenl=0,lenr=0,last;
            for(int i = r,last = flag;i>=min(l,1);last = a[i],i--)
                if(a[i]>last)lenr++;
                else break;
            for(int i = l,last = flag;i<=max(r,n);last = a[i],i++)
                if(a[i]>last)lenl++;
                else break;

            if(lenl>lenr){
                int tmp = lenl;
                while(lenl)
                {
                    ans[top++] = 'L';
                    lenl--;
                }
                l = l+tmp-1;
                flag = a[l];
            }
            else
            {
                int tmp = lenr;
                while(lenr){
                    ans[top++] = 'R';
                    lenr--;
                }
                r = r-tmp+1;
                flag = a[r];
            }
        }
        else if(a[l]>flag&&a[r]<=flag)
        {
            while(l<=r&&a[l]>flag){
                ans[top++] = 'L';
                flag = a[l];
                l++;
            }
        }
        else if(a[r]>flag&&a[l]<=flag)
        {
            while(r>=l&&a[r]>flag){
                ans[top++] = 'R';
                flag = a[r];
                r--;
            }
        }
        else
        {
            break;
        }
    }
    printf("%d\n",top);
    for(int i=0;i<top;i++)
        putchar(ans[i]);
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值