CSU-暑假集训题 Increasing Subsequence (easy version)

题目链接:http://codeforces.com/problemset/problem/1157/C1

思路:

题意就是从序列的左端或者右端拿出数字,组成一个递增的序列。模拟这个过程就能做出来。

AC代码:

#include<iostream>
using namespace std;
int a[200020];
char b[200020];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    int pre=0;
    int count=0;
    for(int i=1,j=n;i<=j;)
    {
        int maxx=max(a[i],a[j]);
        if(pre>maxx)break;
        else{
            count++;
            int less,pos=0;
            if(maxx==a[j])
            {
                less=a[i];
                pos=1;
                
            }else{
                less=a[j];
                pos=2;
            }
            if(pre<less)
            {
                pre=less;
                if(pos==1){
                    i++;
                    b[count]='L';
                }
                else {
                    j--;
                    b[count]='R';
                }
            }else{
                pre=maxx;
                if(pos==1)
                {
                    j--;
                    b[count]='R';
                }
                else{
                    i++;
                    b[count]='L';
                } 
            }
        }
    }
    cout<<count<<endl;
    for(int k=1;k<=count;k++)cout<<b[k];
    cout<<endl;
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/xlbfxx/p/11252296.html