zzuoj 10396: H.Rectangles 【DP】

10396: H.Rectangles

时间限制: 2 Sec  内存限制: 128 MB
提交: 144  解决: 10
[ 提交][ 状态][ 讨论版]

题目描述

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.

样例输入

148 1416 2829 1214 8

样例输出

2

 

思路:把a,b值大的当作长,小的当作边,接下来就是经典dp,不做解释。

注意两个方形相等的时候,还要注意像(4,5)是可以套在(4,6)里面的。

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f
#define MAX 500+10
using namespace std;
struct record
{
    int l,w;
}num[1010];
int dp[1010];
bool cmp(record a,record b)
{
    if(a.l!=b.l)
    return a.l<b.l;
    else
    return a.w<b.w;
}
int main()
{
    int t,n,i,j;
    int len,wide;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&len,&wide);
            dp[i]=1;
            if(len>wide)
            {
                num[i].l=len;
                num[i].w=wide;
            }
            else
            {
                num[i].l=wide;
                num[i].w=len;
            }
        }
        sort(num,num+n,cmp);
        for(i=0;i<n;i++)
        {
            for(j=0;j<i;j++)
            {
                if(num[i].l==num[j].l&&num[i].w==num[j].w)
                continue;
                if(dp[i]<dp[j]+1&&num[i].l>=num[j].l&&num[i].w>=num[j].w)
                {
                    dp[i]=dp[j]+1; 
                }
            }
        }
        sort(dp,dp+n);
        printf("%d\n",dp[n-1]);
    }
    return 0;
}


 

 

 

你可能感兴趣的:(zzuoj 10396: H.Rectangles 【DP】)