CRB and Apple
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 421 Accepted Submission(s): 131
In Codeland there are many apple trees.
One day CRB and his girlfriend decided to eat all apples of one tree.
Each apple on the tree has height and deliciousness.
They decided to gather all apples from top to bottom, so an apple can be gathered only when it has equal or less height than one just gathered before.
When an 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 only when 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 are multiple test cases. The first line of input contains an integer T, indicating the number of 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 two integers Hi and Di indicating the height and deliciousness of i-th apple.
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)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 2010; 4 int n,c[maxn][maxn],Li[maxn],tot; 5 void add(int *T,int i,int val){ 6 while(i <= tot){ 7 T[i] = max(T[i],val); 8 i += i&-i; 9 } 10 } 11 int query(int *T,int i,int ret = 0){ 12 while(i > 0){ 13 ret = max(ret,T[i]); 14 i -= i&-i; 15 } 16 return ret; 17 } 18 struct Apple{ 19 int h,d; 20 bool operator<(const Apple &rhs)const{ 21 if(h == rhs.h) return d > rhs.d; 22 return h < rhs.h; 23 } 24 }A[maxn]; 25 int main(){ 26 int kase; 27 scanf("%d",&kase); 28 while(kase--){ 29 scanf("%d",&n); 30 memset(c,0,sizeof c); 31 for(int i = 0; i < n; ++i){ 32 scanf("%d%d",&A[i].h,&A[i].d); 33 Li[tot++] = A[i].d; 34 } 35 sort(Li,Li + tot); 36 tot = unique(Li,Li + tot) - Li; 37 sort(A,A+n); 38 for(int i = 0; i < n; ++i) 39 A[i].d = tot - (lower_bound(Li,Li + tot,A[i].d)-Li); 40 for(int i = 0; i < n; ++i){ 41 memset(Li,0,sizeof Li); 42 for(int j = 1; j <= tot; ++j) 43 Li[j] = query(c[j],A[i].d) +1; 44 for(int j = 1; j <= tot; ++j){ 45 add(c[j],A[i].d,Li[j]); 46 add(c[A[i].d],j,Li[j]); 47 } 48 } 49 int ret = 0; 50 for(int i = 1; i <= tot; ++i) 51 ret = max(ret,query(c[i],tot)); 52 printf("%d ",ret); 53 } 54 return 0; 55 }