HDU - 5406 CRB and Apple dp + 树状数组
排序之后就变成了求两个不相交的最长LIS, 用树状数组把n ^ 3 的dp 优化成 n ^ 2 * log(n) 就ok 了。
#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 = 1000 + 2; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 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, tot, dp[N][N], hs[N]; struct Bit { int a[N]; void init() { for(int i = 1; i <= tot; i++) { a[i] = 0; } } void modify(int x, int v) { for(int i = x; i <= tot; i += i & -i) { chkmax(a[i], v); } } int preMax(int x) { int ans = 0; for(int i = x; i; i -= i & -i) { chkmax(ans, a[i]); } return ans; } } big[N], sml[N]; PII apple[N]; int a[N]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); tot = 0; for(int i = 1; i <= n; i++) { scanf("%d%d", &apple[i].fi, &apple[i].se); hs[++tot] = apple[i].se; } hs[++tot] = 0; if(n == 1) { puts("1"); continue; } sort(apple + 1, apple + 1 + n, [&](PII a, PII b) { if(a.fi == b.fi) return a.se < b.se; else return a.fi > b.fi; }); sort(hs + 1, hs + 1 + tot); tot = unique(hs + 1, hs + 1 + tot) - hs - 1; for(int i = 0; i <= n; i++) { a[i] = lower_bound(hs + 1, hs + 1 + tot, apple[i].se) - hs; } for(int i = 0; i <= n; i++) { big[i].init(); sml[i].init(); } int ans = 0; for(int i = 1; i <= n; i++) { for(int j = 0; j < i; j++) { dp[i][j] = j ? 2 : 1; chkmax(dp[i][j], sml[j].preMax(a[i]) + 1); chkmax(dp[i][j], big[j].preMax(a[i]) + 1); big[i].modify(a[j], dp[i][j]); sml[j].modify(a[i], dp[i][j]); chkmax(ans, dp[i][j]); } } printf("%d\n", ans); } return 0; } /* */