CF486E LIS of Sequence

洛谷题面

题目大意

给你一个长度为 n 的序列 a1,a2,,an,你需要把这 n 个元素分成三类:1,2,3

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

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

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

题目分析

fi 表示 {a1,a2,,ai}LIS 长度,gi 表示 {ai,ai+1,,an}LIS 长度,len 表示 {a1,a2,,an}LIS 长度。

对于 ai,若 fi+gilen+1,说明 ai 这个点不在整个序列所有的 LIS 方案里。那么 ansi1

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

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

fi,gi 可以用二分、线段树和树状数组在 O(nlogn) 的时间内求得,记录是否出现重复可以用桶或 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;
}

本文作者:Coros_Trusds

本文链接:https://www.cnblogs.com/Coros-Trusds/p/15951459.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Coros_Trusds  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.