码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 486E LIS of Sequence

时间:2019-02-17 23:33:20      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:tps   amp   col   set   second   nbsp   name   ORC   seq   

LIS of Sequence

我们先找出那些肯定不会再LIS里面。

然后我们从前往后扫一次, 当前位置为 i , 看存不存在一个 j 会在lis上并且a[ j ] > a[ i ], 如果满足则 i 能被省掉。

在从后往前扫一遍就做完啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;

int n, Lis, a[N], Llis[N], Rlis[N], f[N], mx, mn;
char ans[N];

int main() {
    scanf("%d", &n);
    ans[n + 1] = \0;
    for(int i = 1; i <= n; i++) ans[i] = 3;
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    memset(f, inf, sizeof(f));
    for(int i = 1; i <= n; i++) {
        int p = lower_bound(f, f + N, a[i]) - f;
        Llis[i] = p + 1;
        f[p] = min(f[p], a[i]);
    }
    memset(f, inf, sizeof(f));
    for(int i = n; i >= 1; i--) {
        int p = lower_bound(f, f + N, -a[i]) - f;
        Rlis[i] = p + 1;
        f[p] = min(f[p], -a[i]);
    }
    for(int i = 1; i <= n; i++)
        Lis = max(Lis, Llis[i] + Rlis[i] - 1);
    for(int i = n; i >= 1; i--)
        if(Llis[i] + Rlis[i] - 1 != Lis) ans[i] = 1;
    mx = 0;
    for(int i = 1; i <= n; i++) {
        if(Llis[i] + Rlis[i] - 1 == Lis) {
            if(mx >= a[i]) ans[i] = 2;
            mx = max(mx, a[i]);
        }
    }
    mn = inf;
    for(int i = n; i >= 1; i--) {
        if(Llis[i] + Rlis[i] - 1 == Lis) {
            if(mn <= a[i]) ans[i] = 2;
            mn = min(mn, a[i]);
        }
    }
    puts(ans + 1);
    return 0;
}

/*
*/

 

Codeforces 486E LIS of Sequence

标签:tps   amp   col   set   second   nbsp   name   ORC   seq   

原文地址:https://www.cnblogs.com/CJLHY/p/10393169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!