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 strictlyincreasing 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 44and 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
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.
esey的思路卡了半天15数据,换了思路ac了
代码:
//Full of love and hope for life
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/
//https://www.cnblogs.com/zzrturist/ //博客园
//https://blog.csdn.net/qq_44134712 //csdn
using namespace std;
const int N=5e3+10;
typedef long long ll;
const int mod=1e9+7;
int a[200010];
string s="";
int l[200010],r[200010];
int main()
{
int n;
cin >> n;
for(int i=0;i<n;i++) {
cin >> a[i];
}
r[0]=l[n-1]=1;
for(int i=1;i<n;i++) {
if(a[i]<a[i-1]){
r[i]=r[i-1]+1;
}
else {
r[i]=1;
}
}
for(int i=n-2;i>=0;i--) {
if(a[i]<a[i+1]) {
l[i]=l[i+1]+1;
}
else {
l[i]=1;
}
}
int i=0;
int j=n-1;
int t=-1;
while(i<=j) {
int mi=min(a[i],a[j]);
int ma=max(a[i],a[j]);
if(ma<=t) {
break;
}
if(mi==ma) {
if(l[i]>r[j]) {
i++;
s+='L';
}
else {
j--;
s+='R';
}
t=mi;
}
else if(mi>t) {
if(a[i]==mi) {
i++;
s+='L';
}
else {
j--;
s+='R';
}
t=mi;
}
else {
if(a[i]==ma) {
i++;
s+='L';
}
else {
j--;
s+='R';
}
t=ma;
}
}
cout << s.size() << endl;
cout << s;
return 0;
}