NYOJ 16 矩形嵌套

题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=16

思路:最长上升子序列

 

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
struct node
{
    int x,y;
}p[1010];
int dp[1010];
bool cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
void solve()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&p[i].x,&p[i].y);
        if(p[i].x>p[i].y)
            swap(p[i].x,p[i].y);
        dp[i]=1;
    }
    sort(p+1,p+n+1,cmp);
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            if(p[i].x<p[j].x&&p[i].y<p[j].y)
                dp[j]=max(dp[i]+1,dp[j]);
    cout<<dp[n]<<endl;
}
int t;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        solve();
    }
    return 0;
}
View Code

 

posted @ 2013-07-13 11:11  over_flow  阅读(173)  评论(0编辑  收藏  举报