矩形嵌套
时间限制:3000 ms | 内存限制:65535 KB
难度:4
- 描写叙述
- 有n个矩形,每个矩形能够用a,b来描写叙述。表示长和宽。矩形X(a,b)能够嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度)。比如(1,5)能够嵌套在(6,2)内,但不能嵌套在(3,4)中。
你的任务是选出尽可能多的矩形排成一行,使得除最后一个外。每个矩形都能够嵌套在下一个矩形内。
- 输入
- 第一行是一个正正数N(0<N<10)。表示測试数据组数,
每组測试数据的第一行是一个正正数n,表示该组測试数据中含有矩形的个数(n<=1000)
随后的n行。每行有两个数a,b(0<a,b<100)。表示矩形的长和宽 - 输出
- 每组測试数据都输出一个数,表示最多符合条件的矩形数目,每组输出占一行
- 例子输入
-
1 10 1 2 2 4 5 8 6 10 7 9 3 1 5 8 12 10 9 7 2 2
- 例子输出
-
5
有向无环图上的的最长路径。
。。
#include<iostream> #include<cstring> using namespace std; #define MAXN 1000 int g[101][101],t[MAXN+1],n; int Max(int a,int b) {return a>b?a:b;} int dp(int i) { int &ans=t[i]; if(ans>0) return ans; ans=1; for(int j=1;j<=n;++j) { if(g[i][j]) ans=Max(ans,dp(j)+1); } return ans; } int main() { int N,a[101],b[101]; int i,j,max; cin>>N; while(N--) { cin>>n; memset(t,0,sizeof(t)); memset(g,0,sizeof(g)); for(i=1;i<=n;i++) cin>>a[i]>>b[i]; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) if((a[i]>a[j]&&b[i]>b[j])||(a[i]>b[j]&&b[i]>a[j])) g[i][j]=1; } max=-1; for(i=1;i<=n;++i) max=Max(dp(i),max); cout<<max<<endl; } return 0; }