nyoj 1255 难度2 第七届河南省程序设计大赛

Rectangles

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述

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.

输入

The first line of input gives a single integer, 1 ≤ T ≤10, the number of test cases. Then follow, for each test case:
* Line 1: a integer N , Given the number ofrectangles N<=100
* Lines 2..N+1: Each line contains two space-separated integers X Y, the sides of the respective rectangle. 1<= X , Y<=5000

输出

Output for each test case , a single line with a integer K , the length of the longest sequence of fitting rectangles.

样例输入

1

4

8 14

16 28

29 12

14 8

样例输出

2

来源

第七届河南省程序设计大赛

上传者

516108736

 这道题不能按简单的贪心做,类似于求最大递增子序列。矩形嵌套问题,要满足长或宽至少有一边小于小于下一个矩形对应的边,另外一条边小于等于下一个矩形对应的另一条边就可以。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
using namespace std;
struct node {
	int l,w;
}a[105];
bool cmp(node a,node b){
	if(a.l!=b.l)
	return a.l<b.l;
	else
	return a.w<b.w;
}
int dp[105];//dp[i] 第i个矩形之前,可以嵌套的矩形最多是多少 
int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		memset(a,0,sizeof a);
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&a[i].l,&a[i].w);
			if(a[i].l<a[i].w)
			swap(a[i].l,a[i].w);
		}
		sort(a,a+n,cmp);
		int cnt=0;
		memset(dp,0,sizeof(dp));
		for(int i=1;i<n;i++)
		{
			for(int j=0;j<i;j++)
			{
				if(((a[j].l<a[i].l&&a[j].w<=a[i].w)||(a[j].l==a[i].l&&a[j].w<a[i].w))&&dp[i]<dp[j]+1)
				{
					dp[i]=dp[j]+1;
				}
			}
			if(cnt<dp[i])
				cnt=dp[i];
		}
		printf("%d\n",cnt+1);
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_36914923/article/details/80375919