CSU-暑假集训题 Increasing Subsequence (hard version)

题目链接:http://codeforces.com/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 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 (1n2105) — the number of elements in a

.

The second line of the input contains n

integers a1,a2,,an (1ai2105), 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.

.

example

    
 
    Input
5
1 2 4 3 2
Output
4
LRRR
Input
7
1 3 5 6 5 4 2
Output
6
LRLRRR
Input
3
2 2 2
Output
1
R
Input
4
1 2 4 3
Output
4
LLRR
 

Note

The first example is described in the problem statement.

思路

当数列两边的数字相等时,就表明从现在开始只能一边选取,即往后全为L或R,所以这时候只要计算从左边往右得到的数列长还是从右往左得到的数列长即可。
如果数列两边数字不相等,比较两端数字,选取小的即可。
数字x与两端数字比较时会有三种情况:
x<L<R
L<x<R
R<x<L
有特殊的数列
6
1 2 3 4 2 3
当两端指针指在3时,从右往左有递增的数列2 4,如果不判断是否已经结束比较,就会出现错误!

AC代码

#include<iostream>
using namespace std;
int a[200020];
char b[200020];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    int pre=0;
    int count=0;
    for(int i=1,j=n;i<=j;)
    {
        int maxx=max(a[i],a[j]);
        if(pre>=maxx)break;
        else{
            count++;
            int less,pos=0;
            if(a[i]==a[j])
            {
                int count1=0,count2=0,flagl=0,flagr=0;
                for(int k=1;;k++)
                {
                    if(a[i+k]>a[i+k-1]&&!flagl)count1++;       //中间可能出现递增的数列,这是个很大的坑!       
                    else flagl=1;
                    if(a[j-k]>a[j-k+1]&&!flagr)count2++;
                    else flagr=1;
                    if(flagl&&flagr)break;
                }
                if(count1<=count2)
                {    
                    for(int i=0;i<=count2;i++)b[i+count]='R';
                    count+=count2;
                }else{
                    for(int i=0;i<=count1;i++)b[i+count]='L';
                    count+=count1;
                }
                break;
            }
            else if(maxx==a[i])
            {
                less=a[j];
                pos=2;
                
                
            }else{
                less=a[i];
                pos=1;
            }
            if(pre<less)
            {
                pre=less;
                if(pos==1){
                    i++;
                    b[count]='L';
                }
                else {
                    j--;
                    b[count]='R';
                }
            }else{
                pre=maxx;
                if(pos==1)
                {
                    j--;
                    b[count]='R';
                }
                else{
                    i++;
                    b[count]='L';
                } 
            }
        }
    }
    cout<<count<<endl;
    for(int k=1;k<=count;k++)cout<<b[k];
    cout<<endl;
    return 0;
} 

 

posted @ 2019-07-26 20:16  小小笼包包  Views(124)  Comments(0)    收藏  举报