NYOJ 1255 Rectangles

来源:互联网 发布:英文图纸翻译软件 编辑:程序博客网 时间:2024/05/29 09:17

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.
样例输入
148 1416 2829 1214 8
样例输出
2
来源
第七届河南省程序设计大赛

这题一眼看去确实很像贪心类似于会场安排类的题,结果wa了两次发现方法肯定不对
应该最长上升子序列的方法因为如果x比较小而y特别大的话这样的点应该舍去的即考虑最优情况,又被坑了一回

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[500];
struct node
{
    int x, y;
}p[500];
bool cmp(struct node a,struct node b)
{
    if(a.x!=b.x)
    {
        return a.x<b.x;
    }
    else
    {
        return a.y<b.y;
    }
}


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>p[i].x>>p[i].y;
            if(p[i].x>p[i].y)
            {
                swap(p[i].x,p[i].y);
            }
        }
        sort(p,p+n,cmp);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            dp[i]=1;
            int flag=0;
            for(int j=0;j<i;j++)
            {
                if(((p[i].x>p[j].x&&p[i].y>=p[j].y)||(p[i].x==p[j].x&&p[i].y>p[j].y))&&dp[j]>flag)
                {
                    flag=dp[j];
                }
            }
            dp[i]+=flag;
        }
        int cnt=1;
        for(int i=0;i<n;i++)
        {
            if(dp[i]>cnt)
            {
                cnt=dp[i];
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

0 0
原创粉丝点击