题目链接

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();}elsebreak;}int len=1;cout<<v.size()<<endl;cout<<a1<<endl;
}

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

  1. Codeforces Round # 555 (Div. 3) C2. Increasing subsequence (complicated version) (贪心)

    题目链接:http://codeforces.com/contest/1157/problem/C2 当左右两边数字相同时,需要判断一下取哪边能得到更长的递增序列 #include <iostr ...

  2. Codeforces Round #555 (Div. 3), problem: (C2) Increasing Subsequence (hard version)【贪心+撞到南墙也不回头】

    题目链接 题目大意 复杂版大意是我们可以从左右两端每次拿走一个数,一直拿,不过要满足一个条件,每次拿的数要保证严格递增(即从小到大然后不会有相同的情况) 复杂版的话是会有相同的数字出现 在题解中正式说 ...

  3. Codeforces Round #555 (Div. 3) c2 d e f

    c2:Increasing Subsequence (hard version) 那边小取那边,然后相等比较后面的长度 #include<bits/stdc++.h> using name ...

  4. 差分 ---- Codeforces Round #672 (Div. 2):C2. Pokémon Army (hard version)[差分的思想]

    题目链接 题目大意:就算给你一序列,按照顺序出若干个数组成一个的序列,然后对这个序列定义一个权值就算奇数位置的和减去偶数位置的和,问你能的到的最大的权值是多少? **a1 - a2 + a3 - a4 ...

  5. Codeforces Round #672 (Div. 2) C2 - Pokémon Army (hard version)(贪心,维护变化值)

    x数组里选一个子数组y(原数组顺序),y1-y2+y3-y4+- 的最大值 然后还有q次交换操作,每次修改之后都要输出新的最大值 (1)如果没有修改,单纯对于当前数组考虑,我们最后选出来的点肯定是波峰 ...

  6. Increasing Subsequence (hard version)

    https://codeforces.com/contest/1157/problem/C2 题意:给一个存在重复的元素的数组,每次可以在头或者尾取一个数,求取数最长严格递增序列的方法 题解:因为如果 ...

  7. Increasing Subsequence (easy version)

    https://codeforces.com/contest/1157/problem/C1 题意:给一个不重复的元素的数组,每次可以在头或者尾取一个数,求取数最长严格递增序列的方法 题解:因为元素不 ...

  8. #658 (Div. 2) C2.Prefix Flip (Hard Version)

    题目描述 This is the hard version of the problem. The difference between the versions is the constraint ...

  9. Codeforces Round #568 (Div. 2)C2. Exam in BerSU (hard version)

    Codeforces Round #568 (Div. 2)C2. Exam in BerSU (hard version) 贪心+暴力 大致题意:N个人考试,每个人花费的时间是a[i],他们总共花费 ...

  10. Codeforces Round #636 (Div. 3) C.Alternating Subsequence

    Codeforces Round #636 (Div. 3) C.Alternating Subsequence 题目链接 Recall that the sequence b is a a subs ...

最新文章

  1. 用c实现跨平台异常捕获机制
  2. android web 打印,Android设备WebView打印Console Log
  3. Understanding G1 GC Logs--转载
  4. 康托展开式---我排第几+逆康托展开
  5. 小微商户申请php,微信小微商户申请入驻 - osc_r8q2esik的个人空间 - OSCHINA - 中文开源技术交流社区...
  6. TCP/IP 通信示例
  7. AJAX 信息查询管理
  8. 容器编排技术 -- Kubernetes Replica Sets
  9. 容器、微服务和互联网架构浅谈
  10. git 查看修改明细_git查看某个文件的修改历史
  11. 提高软件CPU占用率
  12. 圣诞帽php,微信小程序“圣诞帽”的实现方法
  13. Python 利用win32com批量给excel加密
  14. 华师在线计算机网络,华师在线-作业计算机网络.docx
  15. C# 委托代理动态的方法
  16. Divan and bitwise operations(组合数+思维)
  17. MySQL使用group by分组查询每组最新的一笔数据
  18. android 判断图片的格式的,android判断文件是否是图片文件的方法
  19. 云原生架构下的微服务选型和演进
  20. 用C++实现一个小小的爬虫

热门文章

  1. Scikit-Learn之利用高斯过程回归
  2. tableau制作中国地图(全)
  3. java面试中掺水了,java软件工程师工作简历模板下载
  4. 图解hadoop原理
  5. 百度地图javascriptAPI点击地图得到坐标(拾取坐标)
  6. 微信小程序开发者文档 开放文档 地址
  7. 大数据分析案例:财政收入预测Jupyter版
  8. nginx直接打印输出_Nginx 日志打印POST数据
  9. [从零开始学习FPGA编程-22]:进阶篇 - 架构 - FPGA内部硬件电路的设计与建模
  10. 利用YYLabel 进行图文混排+高度计算