CF486E LIS of Sequence
题目大意
给你一个长度为 的序列 ,你需要把这 个元素分成三类::
-
所有的最长上升子序列都不包含这个元素。
-
有但非所有的最长上升子序列包含这个元素。
-
所有的最长上升子序列都包含这个元素。
题目分析
令 表示 的 长度, 表示 的 长度, 表示 的 长度。
对于 ,若 ,说明 这个点不在整个序列所有的 方案里。那么 。
若 ,判断是否存在 且 ,则 一定属于第二种情况:有但非所有的最长上升子序列包含这个元素。因为其他元素和 某种意义上来说等价,都在其 中起到同样的作用。
其他情况为最后一种情况。
可以用二分、线段树和树状数组在 的时间内求得,记录是否出现重复可以用桶或 。树状数组码量小,完爆线段树;树状数组容易理解,完爆二分。故此处选择树状数组。
代码
//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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端