zoukankan      html  css  js  c++  java
  • Rectangles

    Rectangles

    时间限制: 1 Sec 内存限制: 128 MB


    题目描述

    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个箱子的宽和高,宽和高都小于另一个箱子的时候这个箱子可以套进去那个箱子里面,当然又可以容忍一个边一样的时候,也是可以套进去的;

    解题思路

    按照所给边的最大边的值从小打到排序,之后从小开始遍历,使用一个变量c来记录当前套进去的筐子数,然后用两重循环暴力更新,最后找出最大值。

    代码

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stdlib.h>
    #include <queue>
    
    using namespace std;
    
    struct date {
        int a,b,c;
    }line[550];
    int cmp (date a,date b)
    {
        if(a.a!=b.a){
            return a.a<b.a;
        }else {
            return a.b<b.b;
        }
    }
    int main ()
    {
        int t,n,m,i,j;
        int vis[550];
    
        scanf("%d",&t);
        while(t--){
            memset(vis,0,sizeof(vis));
            scanf("%d",&n);
            int a,b;
            for(i=0;i<n;i++){
                scanf("%d %d",&a,&b);
                if(a>b){
                    line[i].a=a;
                    line[i].b=b;
                }else {
                    line[i].b=a;
                    line[i].a=b;
                }
                line[i].c=1;
            }
            sort(line,line+n,cmp);
            int maxx=0,k=0;
            for(i=1;i<n;i++){
                maxx=line[i].c;
                k=-1;
                for(j=i-1;j>=0;j--){
                    if(vis[j]==1||line[j].a>line[i].a||line[j].b>line[i].b)
                        continue;
                    if(line[j].a<line[i].a){
                        if(maxx<line[i].c+line[j].c){
                            k=j;
                            maxx=line[i].c+line[j].c;
                        }
                    }else if(line[j].b<line[i].b){
                        if(maxx<line[i].c+line[j].c){
                            k=j;
                            maxx=line[i].c+line[j].c;
                        }
                    }
                }
                if(k!=-1){
                    //vis[k]=1;
                    line[i].c=maxx;
                }
            }
            maxx=0;
            for(i=0;i<n;i++){
                //printf("%d==  %d  %d
    ",i+1,line[i].c,vis[i]);
                if(vis[i]==0&&line[i].c>maxx){
                    maxx=line[i].c;
                }
            }
            printf("%d
    ",maxx);
        }
        return 0;
    }
    
  • 相关阅读:
    构造方法
    封装 private
    局部变量和成员变量区别
    IOC
    Linux端口占用查询命令
    Nginx小白入门实战
    SQL left join right join inner join之间的区别
    IDEA导入maven工程时,不会自动识别怎么办
    Spring中Controller层中的method显示为灰色并且提示method is never used的原因
    查看服务器公网IP
  • 原文地址:https://www.cnblogs.com/lanaiwanqi/p/10445724.html
Copyright © 2011-2022 走看看