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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章