C1. Increasing Subsequence (easy version)

C1. Increasing Subsequence (easy version)
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
inputCopy
5
2 1 5 4 3
outputCopy
4
LRRR
inputCopy
7
1 3 5 6 7 4 2
outputCopy
7
LRLRLLL
inputCopy
3
1 2 3
outputCopy
3
LLL
inputCopy
4
1 2 4 3
outputCopy
4
LLRL
Note
The first example is described in the problem statement.

贪心,每次取两边较小的且比上次取的数大的数。

有两种做法,一种是题解给的用vector做的,一种是用数组。

1.

#include <bits/stdc++.h>
using namespace std;
int main()
{

    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    string res;
    int l = 0, r = n - 1;
    int lst = 0;
    while (l <= r)
    {
        vector<pair<int, char>> cur;
        if (lst < a[l])
            cur.push_back(make_pair(a[l], 'L'));
        if (lst < a[r])
            cur.push_back(make_pair(a[r], 'R'));
        sort(cur.begin(), cur.end());
        if (int(cur.size()) == 2)
        {
            cur.pop_back();
        }
        if (int(cur.size()) == 1)
        {
            if (cur[0].second == 'L')
            {
                res += 'L';
                lst = a[l];
                ++l;
            }
            else
            {
                res += 'R';
                lst = a[r];
                --r;
            }
        }
        else
        {
            break;
        }
    }

    cout << res.size() << endl << res << endl;

    return 0;
}

2.

#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include <fstream>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    int s[200005];
    string a;
    for(int i=0; i<n; i++)
        scanf("%d",&s[i]);
    int ans=0;
    int l=0,r=n-1;
    if(s[l]<s[r])//找到取的第一个数
    {
        a+='L';
        ans=s[l];
        l++;
    }
    else
    {
        a+='R';
        ans=s[r];
        r--;
    }
    while(l<=r)
    {
        if(ans<s[l]&&ans>s[r])
        {
            a+='L';
            ans=s[l];
            l++;
        }
        else if(ans>s[l]&&ans<s[r])
        {
            a+='R';
            ans=s[r];
            r--;
        }
        if(ans<s[l]&&ans<s[r])
        {
            if(l==r)//特别注意
            {
                a+='L';
                ans=s[l];
                l++;
            }
            else
            {
                if(s[l]<s[r])
                {
                    a+='L';
                    ans=s[l];
                    l++;
                }
                else
                {
                    a+='R';
                    ans=s[r];
                    r--;
                }
            }
        }
        else if(ans>s[l]&&ans>s[r])
            break;
}
    cout<<a.size()<<endl<<a<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值