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. All these integers are distinct, each value from 11 to ?n 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][2,1,5,4,3] the answer is 44 (you take 22 and the sequence becomes [1,5,4,3][1,5,4,3], then you take the rightmost element 33 and the sequence becomes [1,5,4][1,5,4], then you take 44 and the sequence becomes [1,5][1,5] and then you take 55 and the sequence becomes [1][1], the obtained increasing sequence is [2,3,4,5][2,3,4,5]).
Input
The first line of the input contains one integer ?n (1≤?≤2⋅1051≤n≤2⋅105) — the number of elements in ?a.
The second line of the input contains ?n integers ?1,?2,…,??a1,a2,…,an (1≤??≤?1≤ai≤n), where ??ai is the ?i-th element of ?a. All these integers are pairwise distinct.
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 2 1 5 4 3
output
Copy
4 LRRR
input
Copy
7 1 3 5 6 7 4 2
output
Copy
7 LRLRLLL
input
Copy
3 1 2 3
output
Copy
3 LLL
input
Copy
4 1 2 4 3
output
Copy
4 LLRL
Note
The first example is described in the problem statement.
题意:
给定 n 个元素组成的数组,每次只能移动数组最左边或者最右边的数字
移动变成上升序列,即每次移动的数组必须比原来的数字大(已知不可能出现相同的数字)
求可以移动的最长的数组长度以及输出这个数组
如果移动最左边,输出 ' L ' ; 如果移动最右边,输出 ' R '
例如:
5
2 1 5 4 3
依次是 : 2 - 3 - 4 - 5
思路:
用 i 和 j 指针指向 数组最左边和最右边
用 pos 记录下前一个被移动的大小,用 左右两边的数字与 pos 比较
可以有四种情况:
①:如果是结束条件即 : i == j
判断是否比 pos 大,如果大的话需要输出
②:如果左右两边都比pos 大,则尽可能选择一个小的记为 pos ,为后边的选择留出更大的空间
③:如果仅仅左边大,则输出左边,记录新的 pos
④:如果仅仅右边大,则输出右边,记录新的 pos
#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 <deque>
#include <set>
#define INF 0x3f3f3f3f
#define memset(a,b) memset(a,b,sizeof(a))
#define mod 1e6+7;
using namespace std;
typedef long long LL;
const double PI = acos(-1);
const int M = 2e5+10;
int a[M];
string s;
int main()
{
ios::sync_with_stdio(false);
int n;
LL minn;
LL cnt = 0;
cin >> n;
for(int i=0; i<n; i++)
cin >> a[i];
int i=0,j=n-1;
LL pos;
// 先比较 a[0] 与 a[n-1]
if(a[0] < a[n-1]){
pos = a[i];
s += 'L';
i++;
}
else{
pos = a[j];
s += 'R';
j--;
}
while(1)
{
if(i == j){
if(a[i] > pos)
s += 'L';
break;
}
if(a[i] > pos && a[j] > pos)
{
minn = min(a[i],a[j]);
if(minn == a[i]){
pos = a[i];
i++;
s += 'L';
continue;
}
else{
pos = a[j];
j--;
s += 'R';
continue;
}
}
else if(a[i] > pos)
{
pos = a[i];
i++;
s += 'L';
continue;
}
else if(a[j] > pos){
pos = a[j];
j--;
s += 'R';
continue;
}
else
break;
}
cnt = s.size();
cout << cnt << endl;
cout << s << endl;
}