题目链接:http://nyoj.top/problem/1255

  • 内存限制:64MB 时间限制:1000ms

题目描述

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

解题思路

题意:有n个矩阵,如果大的套小的看最多能套几层。大的矩阵必须有一边大于小矩阵的一边,例如2*2的矩阵可以套2*1的矩阵,但是不能套2*2的矩阵,注意矩形是可以旋转的。
思路:首先将矩阵长的边算作x边,小的算y边,然后将n个矩阵按照x的长短排序若x相同则按照y排序,然后跑一遍LIS就行了。

#include <bits/stdc++.h>
using namespace std;
struct edge {
    int l, w;
}e[105];
int dp[105];
bool cmp(edge A, edge B) {
    if (A.l != B.l)
        return A.l < B.l;
    return A.w < B.w;
}
bool Judge(int a, int b) {
    if (e[a].l <= e[b].l && e[a].w < e[b].w)
        return true;
    if (e[a].l < e[b].l && e[a].w <= e[b].w)
        return true;
    return false;
}
int main() {
    int t, n, max_, tempmax_;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &e[i].l, &e[i].w);
            if (e[i].l < e[i].w)
                swap(e[i].l, e[i].w);
        }
        sort(e, e + n, cmp);
        max_ = dp[0] = 1;
        for (int i = 1; i < n; i++) {
            tempmax_ = 0;
            for (int j = 0; j < i; j++) {
                if (Judge(j, i) && tempmax_ < dp[j])
                    tempmax_ = dp[j];
            }
            dp[i] = tempmax_ + 1;
            max_ = max(max_, dp[i]);
        }
        printf("%d\n", max_);
    }
    return 0;
}