HDU - 5324 Boring Class BIT套线段树
直接树套树维护dp, 或者分治 + 树状数组维护。
好像内存卡得比较紧。
#pragma GCC optimize(2) #pragma GCC optimize(3) #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned 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 ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 5e4 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = (int)1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n; int L[N], R[N]; int hsL[N], hsR[N]; int totL, totR; int path[N]; int dp[N]; vector<int> ans; struct Node { int id, ls, rs; } a[N * 80]; int treeTot; inline int newNode() { treeTot++; a[treeTot].id = 0; a[treeTot].ls = 0; a[treeTot].rs = 0; return treeTot; } void update(int p, int id, int l, int r, int &rt) { if(!rt) rt = newNode(); if(dp[a[rt].id] < dp[id] || dp[a[rt].id] == dp[id] && id < a[rt].id) a[rt].id = id; if(l == r) return; int mid = l + r >> 1; if(p <= mid) update(p, id, l, mid, a[rt].ls); else update(p, id, mid + 1, r, a[rt].rs); } int query(int L, int R, int l, int r, int rt) { if(!rt) return 0; if(R < l || r < L || R < L) return 0; if(L <= l && r <= R) return a[rt].id; int mid = l + r >> 1; int id = query(L, R, l, mid, a[rt].ls); int rid = query(L, R, mid + 1, r, a[rt].rs); if(dp[id] < dp[rid] || dp[id] == dp[rid] && id > rid) id = rid; return id; } struct Bit { int a[N]; void init() { for(int i = 1; i <= totL; i++) { a[i] = newNode(); } } void modify(int x, int y, int id) { for(int i = x; i <= totL; i += i & -i) { update(y, id, 1, totR, a[i] ); } } int query(int x, int y) { int id = 0; for(int i = x; i; i -= i & -i) { int rid = ::query(y, totR, 1, totR, a[i]); if(dp[id] < dp[rid] || dp[id] == dp[rid] && id > rid) id = rid; } return id; } } bit; void init() { totL = totR = treeTot = 0; ans.clear(); for(int i = 1; i <= n; i++) { path[i] = 0; } } int main() { while(scanf("%d", &n) != EOF) { init(); for(int i = 1; i <= n; i++) { scanf("%d", &L[i]); hsL[++totL] = L[i]; } for(int i = 1; i <= n; i++) { scanf("%d", &R[i]); hsR[++totR] = R[i]; } sort(hsL + 1, hsL + 1 + totL); sort(hsR + 1, hsR + 1 + totR); totL = unique(hsL + 1, hsL + 1 + totL) - hsL - 1; totR = unique(hsR + 1, hsR + 1 + totR) - hsR - 1; for(int i = 1; i <= n; i++) { L[i] = lower_bound(hsL + 1, hsL + 1 + totL, L[i]) - hsL; R[i] = lower_bound(hsR + 1, hsR + 1 + totR, R[i]) - hsR; } bit.init(); int mx = 0; for(int i = n; i >= 1; i--) { int id = bit.query(L[i], R[i]); dp[i] = 1; if(id > 0) { dp[i] = dp[id] + 1; path[i] = id; } chkmax(mx, dp[i]); bit.modify(L[i], R[i], i); } int start = -1; for(int i = 1; i <= n; i++) { if(dp[i] == mx) { start = i; break; } } for(int i = start; i; i = path[i]) { ans.push_back(i); } printf("%d\n", mx); for(int i = 0; i < SZ(ans); i++) { printf("%d%c", ans[i], " \n"[i == SZ(ans) - 1]); } } return 0; } /* */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步