NYOJ_1225_Rectangles【dp】

来源:互联网 发布:c语言播放音乐linux 编辑:程序博客网 时间:2024/06/14 20:30
/*
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.
样例输入

1
4
8 14
16 28
29 12
14 8

样例输出

2

*/

#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;struct Node{int x;int y;}edge[105];int dp[105];int cmp(const void *a,const void *b)//从小到大排序 {Node *c = (Node *)a;Node *d = (Node *)b;if(c->x != d->x){return c->x-d->x;}else{return c->y-d->y;}}bool judge(Node a,Node b) {if(a.x>b.x && a.y>b.y){  //长宽 都大于后面 return true;}else if(a.x>=b.x && a.y>b.y){  //长相等,宽大于后面 return true;}else if(a.x>b.x && a.y>=b.y){ //长大于后面,宽相等 return true;}return false;}int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);int i,j;for(i=0;i<n;i++){scanf("%d%d",&edge[i].x,&edge[i].y);if(edge[i].x > edge[i].y) // 不符合了,长宽 交换一下位置 {int t = edge[i].x;edge[i].x = edge[i].y;edge[i].y = t;}}qsort(edge,n,sizeof(edge[0]),cmp);//二级排序 int max = 0,Max = 0;memset(dp,0,sizeof(dp));for(i=1;i<n;i++)  //dp,给最长子序列 dp一样 {max = 0;for(j=0;j<i;j++){if( judge(edge[i],edge[j]) && max < dp[j] + 1){max = dp[j] + 1;}}dp[i] = max;if(Max<dp[i]){Max = dp[i];}}printf("%d\n",Max+1);}return 0;}


0 0
原创粉丝点击