CRB and Apple Time limit:12000/6000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 358 Accepted Submission (s): 109
Problem Description in Codeland there is many apple trees.
One day CRB and he girlfriend decided to eat all apples of One tree.
Each apple on the tree has a height and deliciousness.
They decided to gather all apples from top to bottom, so a apple can be gathered if it has equal or less height th An one just gathered before.
When a apple is gathered, they do one of the following actions.
1. CRB eats the apple.
2. His girlfriend eats the apple.
3. Throw the apple away.
CRB (or his girlfriend) can eat the apple if it has equal or greater deliciousness than one he (she) just ate before.
CRB wants to know the maximum total number of apples they can eat.
Can you help him?
Input There is multiple test cases. The first line of input contains an integer T, indicating the number of the test cases. For each test case:
The first line contains a single integer N denoting the number of apples in a tree.
Then N lines follow, i-th of them contains both integers Hi and Di indicating the height and deliciousness of i-th app Le.
1≤t≤48
1≤n≤1000
1≤hi, di≤109
Output for each test case, output the maximum total number of apples they can eat.
Sample Input
1 5 1 1 2 3 3 2 4 3 5 1
Sample Output
4
Author KUT (DPRK)
Source multi-university Training Contest 10
Sort h First, and then find only two non-intersecting longest non-descending subsequence.
Discretization of D. Use Dp[i][j] to represent the current position, the last apple of the two subsequence is the delicious value of I and J case,
The maximum number of apples to eat.
For the current d, the enumeration I transfers the formula: Dp[i][d] = dp[d][i] = Dp[i][j] + 1 (J <= D)
According to the nature of the non-descending subsequence is monotonic, just dp[i][d] = dp[d][i] = max (Dp[i][j]), J<=d
Maintain the maximum value with a tree-like array
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <
Vector> using namespace std;
#define MAXN 1001 int TREE[MAXN][MAXN];
int m;
void Add (int *t,int p,int N) {for (;p <=m;p+=p&-p) t[p] = max (t[p],n);}
int query (int *t,int p) {int ans = 0;
for (;p >0;p-=p&-p) ans = max (ans,t[p]);
return ans;
} struct node{int h,d;};
Node p[1001];
int comp (Node A,node b) {if (a.h = = b.h) return A.D > B.D;
return A.H < B.h;
} int HAHA[MAXN];
int main () {int t,n,h,d;
Freopen ("1001.in", "R", stdin);
Freopen ("1001x.out", "w", stdout);
scanf ("%d", &t);
while (t--) memset (tree,0,sizeof (tree));
scanf ("%d", &n);
for (int i = 0; i < n; i++) {scanf ("%d%d", &P[I].H,&P[I].D);
Haha[i] = P[I].D;
} sort (haha,haha+n);
m = Unique (haha,haha+n)-haha;
Sort (p,p+n,comp); for (int i =0;i < n;
i++) P[I].D = m (Lower_bound (HAHA,HAHA+M,P[I].D)-haha);
int d,u,v;
for (int i = 0;i < n; i++) {memset (haha,0,sizeof (haha));
D = P[I].D;
for (int j = 1;j <= m; j + +) Haha[j] = query (tree[j],d) +1;
for (int j = 1;j <= m; j + +) {Add (Tree[j],d,haha[j]);
Add (Tree[d],j,haha[j]);
}} int ans = 0;
for (int i = 1;i <= m; i++) ans = max (ans,query (tree[i],m));
printf ("%d\n", ans);
} return 0;
}