Last year's multi-school questions, a long time ago wanted to fill, but the IQ is limited, at the outset even the problem code are not understand.
—————————————————————————————————————————————————————————————————————
Test instructions converted to: given n number, find two longest disjoint lis.
First of all, the classic problem of an LIS Nlogn practice. Enumerates the current number, if it is larger than the end number, inserts the end, or two points to find, insert the appropriate position.
With this problem, we have a method of solving the LIS using a tree-like array or a segment tree +DP.
First discretization. Dp[n] Indicates the length of the LIS when the end number is n.
For loop enumeration number of i,dp[i] = max (Dp[j]) +1, J <= I
The value of Max can be resolved with a tree-like array or segment tree Logn complexity, and the total complexity remains nlogn.
Well, can it be solved with a tree-like array?
Tree-like array of two operations, single-point modification, prefix query.
We find that the value of dp[i] is monotonous. Each time you modify it, you can pass the update up with a tree-like array. (If the value of dp[i] becomes smaller, the update cannot be passed up. )
And our query satisfies J <= I exactly is the prefix query.
The tree-like array is perfectly solved.
—————————————————————————————————————————————————————————————————————
Anyway
How to solve two lis?
DP[I][J] represents the current DP value at the end of the two Lis at I and J.
DP[I][J] = max (Dp[i][k]) +1, K <= J;
All we have to do is to have the answer in an array and then uniformly update the row and column values of the DP array.
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5 using namespacestd;6 7 structp{8 inth, D;9 p () {}TenPintHintd): H (H), D (d) {} One BOOL operator< (Constp& x)Const{ A returnH! = x.h? H > X.h:d <x.d; - } - }; theP pp[1005]; - ints[2005]; - intdp[1005][1005], tmp[1005]; - + voidAddintAintXintd) { - for(inti = x; I <1005; i + = i&-i) +A[i] =Max (A[i], d); A } at intSumintAintx) { - intRET =0; - for(inti = x; I I-= i&-i) -RET =Max (ret, a[i]); - returnret; - } in - intMain () { to intT, N; scanf"%d", &t); + while(t--){ -scanf"%d", &n); the for(inti =0; I < n; i++){ *scanf"%d%d", &pp[i].h, &pp[i].d); $S[i] =pp[i].d;Panax Notoginseng } -Sort (PP, pp+n); theSort (S, s+n); + for(inti =0; I < n; i++) APP[I].D = Lower_bound (S, S+n, PP[I].D)-s+1; the +Memset (DP,0,sizeof(DP)); - intAns =0; $ for(inti =0; I < n; i++){ $ intv =pp[i].d; - for(intj =1; J <= N; J + +){ -TMP[J] = SUM (dp[j], V) +1; theAns =Max (ans, tmp[j]); - }Wuyi for(intj =1; J <= N; J + +){ the Add (Dp[j], V, Tmp[j]); - Add (Dp[v], J, Tmp[j]); Wu } - } Aboutprintf"%d\n", ans); $ } - return 0; -}View Code
hdu5406 CRB and Apple dp+ two Lis