1. 程式人生 > >Codeforces 486E LIS of Sequence

Codeforces 486E LIS of Sequence

puts unsigned min class sig ++ cond sizeof size

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