Codeforces #555 (Div. 3)--C2 Increasing Subsequence (hard version)--投石问路+deque/双指针

题目链接

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 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][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 44 and 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

Copy

5
1 2 4 3 2

output

Copy

4
LRRR

input

Copy

7
1 3 5 6 5 4 2

output

Copy

6
LRLRRR

input

Copy

3
2 2 2

output

Copy

1
R

input

Copy

4
1 2 4 3

output

Copy

4
LLRR

Note

The first example is described in the problem statement.

与C1不同的是,序列存在重复元素,不能一条路走到黑,需要投石问路,有点像搜索预测性剪枝。如果两边相等,由于每次都要取比原来大的,只能一边走到底,另一边永远取不到

这道题双指针更好些,因为判断相等的时候,试探模拟的时候,不会改变整个序列,而deque每次都要复制作为临时队列。

#include <bits/stdc++.h>
using namespace std;
deque<int>q,qt;//也可用双指针l,r模拟 
string a1;
int main()
{
  int n;
  cin>>n;
  int a;
  for(int i=0;i<n;i++){
  	scanf("%d",&a);
  	q.push_back(a);
  }

  vector<int>v;
  int t=0;
  while(!q.empty()){
  
  	if(q.front()>t&&q.back()>t){
  		if(q.front()==q.back()){//特判队首队尾相等的情况
  			int num1=0;
			  int num2=0;
			  int t1=t;
			  for(int i=0;i<q.size();i++)qt.push_back(q[i]);//厉害!还支持【】 
  			while(!qt.empty()){  //用个临时队列qt,投石问路
  				if(qt.front()>t1){
				    num1++;
			  		t1=qt.front();
			  		qt.pop_front();
				  }
				  else break;
			  }
			  qt.clear();
			  for(int i=0;i<q.size();i++)qt.push_back(q[i]);//清空
			  t1=t;
			  while(!qt.empty()){
			  	if(qt.back()>t1){
			  			num2++;
				  		t1=qt.back();
				  		qt.pop_back();
					  }
					  else break;
			  }
			  qt.clear();
			  if(num1>num2){   //比较哪个更长,这时候才真正开始操作
			  	while(num1--){
			  		v.push_back(q.front());
		  			a1+="L";
  					t=q.front();
  					q.pop_front();
				  }
			  }
			  else{
			  	while(num2--){
			  		v.push_back(q.back());
		  		    a1+="R";
		  			t=q.back();
		  			q.pop_back();
				  }
			  }
		  } 
  		else if(q.front()<q.back()){
  			v.push_back(q.front());
  		    a1+="L";
  			t=q.front();
  			q.pop_front();
		  }
		  else{
		  	v.push_back(q.back());
		  	a1+="R";
  		t=q.back();
  		q.pop_back();
		  }
  		
  		
	  }
	  else if(q.back()>t){
	  		v.push_back(q.back());
	  		a1+="R";
  		t=q.back();
  		q.pop_back();
	  }
	  else  if(q.front()>t){
	  	v.push_back(q.front());
  		a1+="L";
  		t=q.front();
  		q.pop_front();
	  }
	  else
	  break;
  }
  int len=1;
  cout<<v.size()<<endl;

  cout<<a1<<endl;
}

猜你喜欢

转载自blog.csdn.net/zjyang12345/article/details/89604682
今日推荐