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 ? consisting of ? integers. All these integers are distinct, each value from 1 to ? appears in the sequence exactly once.
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 [2,1,5,4,3] the answer is 4 (you take 2 and the sequence becomes [1,5,4,3], then you take the rightmost element 3 and the sequence becomes [1,5,4], then you take 4 and the sequence becomes [1,5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2,3,4,5]).
Input
The first line of the input contains one integer ? (1≤?≤2⋅105) — the number of elements in ?.
The second line of the input contains ? integers ?1,?2,…,?? (1≤??≤?), where ?? is the ?-th element of ?. All these integers are pairwise distinct.
Output
In the first line of the output print ? — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string ? of length ?, where the ?-th character of this string ?? should be 'L' if you take the leftmost element during the ?-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
input
5
2 1 5 4 3
output
4
LRRR
input
7
1 3 5 6 7 4 2
output
7
LRLRLLL
input
3
1 2 3
output
3
LLL
input
4
1 2 4 3
output
4
LLRL
Note
The first example is described in the problem statement.
题意:判断一个递增的串,不一定是加1
思路:每次找一个两边最小的,每次往里走,直到走不通。注意走到最中间的时候(l==r)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int M=2e5+10;
const int MAX=1e18+10;
const double PI = acos(-1);
int a[M];
char s[M];
int main()
{
int n,i,j,ans=0,p,q,e=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int l=0,r=n-1;
p=0;
while(1)
{
if(l>r)
break;
if(l==r&&a[l]>p)
{
ans++;
s[e++]='L';
}
if(a[l]<a[r])
{
if(a[l]>p)
{
ans++;
s[e++]='L';
p=a[l];
l++;
}
else
if(a[r]>p)
{
ans++;
s[e++]='R';
p=a[r];
r--;
}
else
break;
}
else
if(a[r]<a[l])
{
if(a[r]>p)
{
ans++;
s[e++]='R';
p=a[r];
r--;
}
else
if(a[l]>p)
{
ans++;
s[e++]='L';
p=a[l];
l++;
}
else
break;
}
else
break;
}
printf("%d\n%s\n",ans,s);
return 0;
}
//100
//2 3 4 6 7 8 9 12 13 14 15 16 17 19 20 21 24 25 27 28 31 32 34 40 42 43 45 46 47 49 54 55 58 59 60 61 62 63 68 71 74 75 80 81 82 85 86 87 88 89 91 92 95 96 98 99 100 97 94 93 90 84 83 79 78 77 76 73 72 70 69 67 66 65 64 57 56 53 52 51 50 48 44 41 39 38 37 36 35 33 30 29 26 23 22 18 11 10 5 1