您的位置:首页 > 其它

NYOJ 16 矩形嵌套

2013-07-13 11:11 267 查看
题目: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
<<endl;
}
int t;
int main()
{
scanf("%d",&t);
while(t--)
{
solve();
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: