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 aa consisting of nn integers. All these integers are distinct, each value from 11 to nn 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 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≤n1≤ai≤n), where aiai is the ii-th element of aa. All these integers are pairwise distinct.
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 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.
题意:对一组数据,你可以取头或取尾来构成一个递增数列,取一个那么去掉原数组那个。
思路:用双指针,i从头往后,j从尾往后,若头和尾都大于上一个取出来的数st,那么取头和尾中最小的那个,如果头大于st,尾小于st,那么st=头,反之st=尾。结束条件是i >j或者头和尾都小于st,这样头和尾都不能取了。
- #include
- #include
- using namespace std;
- const int N=2e5+10;
- int n;
- int a[N];
- char x;
- queue<int>qu;
- string s,str;
- char st;
- int main()
- {
- cin>>n;
- int st=0;
- for(int i=1;i<=n;i++)cin>>a[i];
- int i=1,j=n;
- while(i<=j)
- {
- if(a[i]>=st&&a[j]>=st)
- {
- if(a[i]<=a[j])
- {
- st=a[i];
- i++;
- str+='L';
-
- }
- else
- {
- st=a[j];
- j--;
- str+='R';
-
- }
- }
- else if(a[i]>=st&&a[j]
- {
- st=a[i];
- i++;
- str+='L';
-
- }
- else if(a[i]
=st) - {
- st=a[j];
- j--;
- str+='R';
-
- }
- else if((a[i]
j) - {
- break;
- }
- }
- cout<
size()< - cout<
- }
┭┮﹏┭┮,可算把这题给想出来了。
-
相关阅读:
MetaGPT: Merging Large Language Models Using Model Exclusive Task Arithmetic
【Spring】AOP 统一问题处理
【tio-websocket】10、单条TCP连接上下文—ChannelContext
大数据的薪资怎么样?想学大数据了?
基于Vue3+TS的Monorepo前端项目架构设计与实现
tomcat 配置ssl
C++算法之旅、04 基础篇 | 第一章 基础算法
android 完全退出应用程序
披头士最经典的那张唱片封面,竟是传奇哈苏所拍
1.(vue3.x+vite)创建工程
-
原文地址:https://blog.csdn.net/qq_62079079/article/details/126004903
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU