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 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, Lis, a[N], Llis[N], Rlis[N], f[N], mx, mn;
char ans[N]; int main() {
scanf("%d", &n);
ans[n + ] = '\0';
for(int i = ; i <= n; i++) ans[i] = '';
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
memset(f, inf, sizeof(f));
for(int i = ; i <= n; i++) {
int p = lower_bound(f, f + N, a[i]) - f;
Llis[i] = p + ;
f[p] = min(f[p], a[i]);
}
memset(f, inf, sizeof(f));
for(int i = n; i >= ; i--) {
int p = lower_bound(f, f + N, -a[i]) - f;
Rlis[i] = p + ;
f[p] = min(f[p], -a[i]);
}
for(int i = ; i <= n; i++)
Lis = max(Lis, Llis[i] + Rlis[i] - );
for(int i = n; i >= ; i--)
if(Llis[i] + Rlis[i] - != Lis) ans[i] = '';
mx = ;
for(int i = ; i <= n; i++) {
if(Llis[i] + Rlis[i] - == Lis) {
if(mx >= a[i]) ans[i] = '';
mx = max(mx, a[i]);
}
}
mn = inf;
for(int i = n; i >= ; i--) {
if(Llis[i] + Rlis[i] - == Lis) {
if(mn <= a[i]) ans[i] = '';
mn = min(mn, a[i]);
}
}
puts(ans + );
return ;
} /*
*/
上一篇:Codeforces Round #277 (Div. 2) 题解


下一篇:Android成长日记-Fragment