Problem Description
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

题意:略

解法:

<nobr> dp[i][j]ij. </nobr>

<nobr> dp[i][j]=max(max(dp[i][k]),max(dp[k][j]))+1 </nobr>

<nobr> dp[i] </nobr>

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n;
struct node{
    int h, d;
    void read()
    {
        scanf("%d%d",&h,&d);
    }
}a[1010];
bool cmp(node x, node y)
{
    if(x.h!=y.h) return x.h>y.h;
    return x.d<y.d;
}
int dp[1010][1010];
int temp[1010];
//dp[i][j]表示上一次男女吃的deliciousness分别为i,j的时候吃的最多的苹果
//dp[i][j] = max(dp[i][k]+1), 0<k<=j
//dp[i][j] = max(dp[k][j]+1), 0<k<=i
namespace BIT{
    int tree[maxn][maxn];
    void init(){
        memset(tree, 0, sizeof(tree));
    }
    inline int lowbit(int x)
    {
        return x&-x;
    }
    void update(int id, int x, int v)
    {
        while(x<maxn){
            tree[id][x]=max(tree[id][x], v);
            x+=lowbit(x);
        }
    }
    int query(int id, int x){
        int ret = 0;
        while(x>0){
            ret = max(ret, tree[id][x]);
            x -= lowbit(x);
        }
        return ret;
    }
}
vector <int> v;
using namespace BIT;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        v.clear();
        init();
        scanf("%d", &n);
        for(int i=1; i<=n; i++){
            a[i].read();
            v.push_back(a[i].d);
        }
        sort(a+1,a+n+1,cmp);
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(),v.end()),v.end());
        for(int i=1; i<=n; i++) a[i].d = lower_bound(v.begin(), v.end(), a[i].d)-v.begin()+1;
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                temp[j] = query(a[i].d, j)+1;
                temp[j] = max(temp[j], query(j, a[i].d)+1);
            }
            for(int j=1; j<=n; j++){
                update(j, a[i].d, temp[j]);
                update(a[i].d, j, temp[j]);
            }
        }
        int ans = 0;
        for(int i=1; i<=n; i++){
            ans = max(ans, query(i, v.size()));
        }
        printf("%d\n", ans);
    }
    return 0;
}