time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 a consisting of n integers. All these integers are distinct, each value from 1 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] 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 n (1≤n≤2⋅105) — the number of elements in a.
The second line of the input contains n integers a1,a2,…,an (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
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.
代码:
#include<iostream>
#include <ostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define dd double
#define mes(x,y) memset(x,y,sizeof(y))
using namespace std;
int main(){
int n;
cin>>n;
char b[200030];
vector<int>a;int v;
for(int i=0;i<n;i++){
cin>>v;
a.push_back(v);
}
int l=0,r=n-1;
int sum=0,i=0,c=0;
while(l<=r){//判断要先看两都是不是都比刚才选的大,是,再比较大小,不是,就直接录入就行了
if(a[l]<c&&a[r]<c)break; //如果两个都比刚才选的小那么就无法进行选择,直接出去就行
if(a[l]<=a[r]&&a[l]>c||a[r]<c){
c=a[l++];sum++;b[i++]='L';
}//录入左边的,只有左边的小于右边的并且比刚才选的数大,或者右边的比刚才选的数小
else{//同理
c=a[r--];sum++;b[i++]='R';
}
}
cout<<sum<<endl;
for(int i=0;i<sum;i++)cout<<b[i];
return 0;
}