Codeforces 486E LIS of Sequence

Posted cjlhy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 486E LIS of Sequence相关的知识,希望对你有一定的参考价值。

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] = ;
    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的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces486 E. LIS of Sequence(LIS唯一性判断)

Codeforces 1304D. Shortest and Longest LIS

codeforces 629D 树状数组+LIS

CodeForces - 583D Once Again... LIS 循环

Codeforces 340D Bubble Sort Graph 规律+LIS

hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)