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

题目链接:http://codeforces.com/contest/1157/problem/C2

当左右两边数字相同时,需要判断一下取哪边能得到更长的递增序列

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <climits>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
static const int MAX_N = 2e5 + 5;
static const ll Mod = 2009;
char str[MAX_N];
int a[MAX_N];
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
        int s = 0, e = n - 1, cnt = 0, prev = 0;   //prev为序列中最大值
        while(s <= e){
            if(a[s] <= prev){
                while(a[e] > prev && e >= s){
                    prev = a[e--];
                    str[cnt++] = 'R';
                }
                break;
            }
            if(a[e] <= prev){
                while(a[s] > prev && s <= e){
                    prev = a[s++];
                    str[cnt++] = 'L';
                }
                break;
            }
            if(a[s] < a[e] && a[s] > prev){
                prev = a[s++];
                str[cnt++] = 'L';
                continue;
            }
            if(a[s] > a[e] && a[e] > prev){
                prev = a[e--];
                str[cnt++] = 'R';
                continue;
            }
            int e1 = 0, e2 = 0;
            if(a[s] > prev){
                e1 = 1;
                while(s + e1 <= e && a[s + e1] > a[s + e1 - 1]) ++e1;
            }
            if(a[e] > prev){
                e2 = 1;
                while(e - e2 >= s && a[e - e2] > a[e - e2 + 1]) ++e2;
            }
            if(e1 > e2){
                prev = a[s++];
                str[cnt++] = 'L';
            }
            else{
                prev = a[e--];
                str[cnt++] = 'R';
            }
        }
        str[cnt] = '\0';
        printf("%d\n%s\n", cnt, str);
    }
}
int main() {
    solve();
    return 0;
}
View Code

 

posted @ 2019-06-01 16:17  html_11  阅读(114)  评论(0编辑  收藏  举报