zzuoj 10396: H.Rectangles 【DP】

3023 단어

10396: H.Rectangles

시간 제한: 2 Sec 메모리 제한: 128MB
제출: 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) 안에 끼울 수 있다는 점도 주의해야 한다.
 
#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
",dp[n-1]); } return 0; }

 
 

좋은 웹페이지 즐겨찾기