CF486E LIS of Sequence

洛谷题面

题目大意

给你一个长度为 \(n\) 的序列 \(a_1,a_2,\cdots,a_n\),你需要把这 \(n\) 个元素分成三类:\(1,2,3\)

  1. 所有的最长上升子序列都不包含这个元素。

  2. 有但非所有的最长上升子序列包含这个元素。

  3. 所有的最长上升子序列都包含这个元素。

题目分析

\(f_i\) 表示 \(\{a_1,a_2,\cdots,a_i\}\)\(\rm LIS\) 长度,\(g_i\) 表示 \(\{a_i,a_{i+1},\cdots,a_n\}\)\(\rm LIS\) 长度,\(len\) 表示 \(\{a_1,a_2,\cdots,a_n\}\)\(\rm LIS\) 长度。

对于 \(a_i\),若 \(f_i+g_i\neq len+1\),说明 \(a_i\) 这个点不在整个序列所有的 \(\rm LIS\) 方案里。那么 \(ans_i\gets 1\)

\(f_i+g_i=len+1\),判断是否存在 \(f_i=f_j\)\(g_i=g_j(i\neq j)\),则 \(a_i\) 一定属于第二种情况:有但非所有的最长上升子序列包含这个元素。因为其他元素和 \(a_i\) 某种意义上来说等价,都在其 \(\rm LIS\) 中起到同样的作用。

其他情况为最后一种情况。

\(f_i,g_i\) 可以用二分、线段树和树状数组在 \(\mathcal{O(n\log n)}\) 的时间内求得,记录是否出现重复可以用桶或 \(map\)。树状数组码量小,完爆线段树;树状数组容易理解,完爆二分。故此处选择树状数组。

代码

//2022/2/28 
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#include <map>
#define enter() putchar(10)
#define debug(c,que) cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) cout << arr[i] << w;
#define speed_up() cin.tie(0),cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define Abs(x) ((x) > 0 ? (x) : (-x))
const int mod = 1e9 + 7;
inline int MOD(int x) {
	while (x < 0) x += mod;
	while (x >= mod) x -= mod;
	return x;
}
namespace Newstd {
	char buf[1 << 21],*p1 = buf,*p2 = buf;
	inline int getc() {
		return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1 ++;
	}
	inline int read() {
		int ret = 0,f = 0;char ch = getc();
		while (!isdigit(ch)) {
			if(ch == '-') f = 1;
			ch = getc();
		}
		while (isdigit(ch)) {
			ret = (ret << 3) + (ret << 1) + ch - 48;
			ch = getc();
		}
		return f ? -ret : ret;
	}
	inline void write(int x) {
		if(x < 0) {
			putchar('-');
			x = -x;
		}
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
}
using namespace Newstd;
using namespace std;

const int ma = 1e5 + 5;
int a[ma],b[ma],f[ma],g[ma],ans[ma];
map<int,int>mp[ma];
int n;
struct BIT {
	int tr[ma];
	#define lowbit(x) (x & -x)
	inline void update(int x,int k) {
		for (;x < ma;x += lowbit(x)) {
			tr[x] = max(tr[x],k);
		}
	}
	inline int query(int x) {
		int res = 0;
		for (;x;x -= lowbit(x)) {
			res = max(res,tr[x]);
		}
		return res;
	}
	inline int getsum(int l,int r) {
		return query(r) - query(l - 1); 
	}
	#undef lowbit
} bita,bitb;
int main(void) {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	n = read();
	for (register int i = 1;i <= n; ++ i) a[i] = read();
	for (register int i = 1;i <= n; ++ i) b[i] = ma - a[i];//方便求 g[i],直接将 a 序列的元素大小关系颠倒过来
	int maxx = 0;
	for (register int i = 1;i <= n; ++ i) {
		f[i] = bita.getsum(1,a[i] - 1) + 1;
		bita.update(a[i],f[i]);
		maxx = max(maxx,f[i]);
	}
	for (register int i = n;i >= 1; -- i) {
		g[i] = bitb.getsum(1,b[i] - 1) + 1;
		bitb.update(b[i],g[i]);
	}
	for (register int i = 1;i <= n; ++ i) {
		if (f[i] + g[i] == maxx + 1) {
			mp[f[i]][g[i]] ++;
		} else {//a[i] 不在任何最长上升子序列
			ans[i] = 1;
		}
	}
	for (register int i = 1;i <= n; ++ i) {
		if (f[i] + g[i] == maxx + 1) {
			if (mp[f[i]][g[i]] >= 2) {
				ans[i] = 2;
			} else {
				ans[i] = 3;
			}
		}
	}
	for (register int i = 1;i <= n; ++ i) printf("%d",ans[i]);
	
	return 0;
}
posted @ 2022-03-01 17:32  Coros_Trusds  阅读(34)  评论(0编辑  收藏  举报