问题 H: Rectangles
时间限制: 1 Sec 内存限制: 64 MB提交: 13 解决: 3
[ 提交][ 状态][ 讨论版]
题目描述
Given N (4 <= N <= 100) rectangles and the lengths of their sides ( integers in the range 1..1,000), write a program that finds the maximum K for which there is a sequence of K of the given rectangles that can "nest", (i.e., some sequence P1, P2, ..., Pk, such that P1 can completely fit into P2, P2 can completely fit into P3, etc.).
A rectangle fits inside another rectangle if one of its sides is strictly smaller than the other rectangle's and the remaining side is no larger. If two rectangles are identical they are considered not to fit into each other. For example, a 2*1 rectangle fits in a 2*2 rectangle, but not in another 2*1 rectangle.
The list can be created from rectangles in any order and in either orientation.
输入
输出
样例输入
1
4
8 14
16 28
29 12
14 8
样例输出
2
题意:给你n个长方体,每个长方体有长宽,让你找出一列的长方体,使得前面一个能被后一个嵌套,相同长和相同宽的长方体不可以嵌套。和最长递增子序列一样,只不过
加点条件而已。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int chang,kuan; } s[105]; bool cmp(node a,node b) { if(a.chang!=b.chang) return a.chang<b.chang; else return a.kuan<b.kuan; } int dp[105]; int vis[1005][1005]; int main() { int T; scanf("%d",&T); while(T--) { memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); int N; scanf("%d",&N); int n=0; for(int i=0; i<N; i++) { int a,b; scanf("%d%d",&a,&b);//把值大的当做长,小的当做宽 if(!vis[a][b])//出现相同规格的筛掉 { if(a<b) { s[n].kuan=b; s[n].chang=a; } else { s[n].chang=b; s[n].kuan=a; } n++; vis[a][b]=1; vis[b][a]=1; } } sort(s,s+n,cmp); int maxn=0; for(int i=0; i<n; i++) { dp[i]=1; for(int j=0; j<i; j++) { if((s[i].chang>=s[j].chang&&s[i].kuan>s[j].kuan&&dp[i]<dp[j]+1)||(s[i].chang>s[j].chang&&s[i].kuan>=s[j].kuan&&dp[i]<dp[j]+1)) dp[i]=dp[j]+1; maxn=max(maxn,dp[i]); } } printf("%d\n",maxn); } return 0; }