码迷,mamicode.com
首页 > 其他好文 > 详细

NYIST-16

时间:2014-09-24 15:57:57      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   div   2014   sp   

/********************************************************************
@file     Main_practise.cpp
@date     2014-9-24
@author   Tiger
@brief    动态规划
@details  矩形嵌套问题
********************************************************************/
#include <cstdio>
#include <algorithm>

typedef struct Rectangle
{
    int m_nLen;
    int m_nWid;
}RECT, *PRECT;

#define MAX_N 1005
int Memo[MAX_N][MAX_N];
int DP[MAX_N];

bool isIn(RECT left_rect, RECT right_rect);
int dp(int i, int nMax);

int main(int argc, const char* argv[])
{
    int nTestCases = 0;
    scanf("%d", &nTestCases);
    
    while (nTestCases--)
    {
        int nRectCnt = 0;
        scanf("%d", &nRectCnt);

        int nLen = 0, nWid = 0;
        PRECT RectArray = new RECT[nRectCnt];
        for (int i=0; i<nRectCnt; ++i)
        {
            scanf("%d%d", &nLen, &nWid);
            RectArray[i].m_nLen = nLen;
            RectArray[i].m_nWid = nWid;

            DP[i] = 0;
            for (int j=0; j<nRectCnt; ++j)
            {
                Memo[i][j] = 0;
            }
        }
        
        for (int i=0; i<nRectCnt; ++i)
        {
            for (int j=0; j<nRectCnt; ++j)
            {
                if (isIn(RectArray[i], RectArray[j]))
                {
                    Memo[i][j] = 1;
                }
            }
        }

        int nMax = 0;
        for (int i=0; i<nRectCnt; ++i)
        {
            nMax = std::max(nMax, dp(i, nRectCnt));
        }
        printf("%d\n", nMax);

        delete [] RectArray;
    }

    return 0;
}

bool isIn(RECT left_rect, RECT right_rect)
{
    return (left_rect.m_nWid > right_rect.m_nWid && left_rect.m_nLen > right_rect.m_nLen)
        || (left_rect.m_nWid > right_rect.m_nLen && left_rect.m_nLen > right_rect.m_nWid);
}

int dp(int i, int nMax)
{
    if (DP[i] > 0)
    {
        return DP[i];
    }
    else
    {
        DP[i] = 1;
        for (int j=0; j<nMax; ++j)
        {
            if (Memo[i][j])
            {
                DP[i] = std::max(DP[i], dp(j, nMax)+1);
            }
        }
        return DP[i];
    }
}

 

NYIST-16

标签:style   blog   color   io   ar   for   div   2014   sp   

原文地址:http://www.cnblogs.com/roronoa-zoro-zrh/p/3990504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!