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\n",i+1,line[i].c,vis[i]);if(vis[i]==0&&line[i].c>maxx){maxx=line[i].c;}}printf("%d\n",maxx);}return 0;
}

转载于:https://www.cnblogs.com/lanaiwanqi/p/10445724.html

Rectangles相关推荐

  1. R语言ggplot2可视化、在可视化区域中自定义添加多个大小不同矩形阴影区域、自定义配置大小不同矩形阴影区域的颜色(Adding multiple shadows/rectangles)

    R语言ggplot2可视化.在可视化区域中自定义添加多个大小不同矩形阴影区域.自定义配置大小不同矩形阴影区域的颜色(Adding multiple shadows/rectangles) 目录

  2. matplotlib可视化基本散点图、在图像多个指定区域绘制方框、进行自定义色彩填充(Draw Multiple Rectangles)

    matplotlib可视化基本散点图.在图像多个指定区域绘制方框.进行自定义色彩填充(Draw Multiple Rectangles) 目录

  3. Codeforces 372 B. Counting Rectangles is Fun

    $ >Codeforces \space 372 B.  Counting Rectangles is Fun<$ 题目大意 : 给出一个 \(n \times m\) 的 \(01\) ...

  4. Codeforces-gym-101020 problem C. Rectangles

    题目链接:http://codeforces.com/gym/101020/problem/C C. Rectangles time limit per test 2.0 s memory limit ...

  5. leetcode 497, 528. Random Point in Non-overlapping Rectangles | 497. 非重叠矩形中的随机点(Java)

    497. Random Point in Non-overlapping Rectangles https://leetcode.com/problems/random-point-in-non-ov ...

  6. 【CodeForces - 764D】Timofey and rectangles (四色定理 + 找规律 + 构造)

    题干: One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane ...

  7. CSU 1204 Rectangles

    CSU 1204 Rectangles Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1011 Solved: 161 Description 如果限定 ...

  8. AIM Tech Round 5C. Rectangles 思维

    C. Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. Drawing Rectangles(绘制矩形)

    绘制过程: 创建路径 -CGMutablePathRef  CGPathCreateMutable (void);  //CGMutablePathRef path = CGPathCreateMut ...

最新文章

  1. netfilter与iptables表规则建立
  2. 架构师的能力模型(图)
  3. 深度学习框架的比较(MXNet, Caffe, TensorFlow, Torch, Theano)
  4. 關於BigDecimal的比較
  5. F - Heron and His Triangle UVALive - 8206
  6. java day_Java_Day7(上)
  7. [Godot][Blender] 通过 Blender 将 fbx 批量转为 gltf 用于 godot
  8. android studio打印日志语句Log.d()
  9. Eclipse用法和技巧十三:自动生成的TODO注释1
  10. Windows下如何实现Ruby操作MongoDB(环境安装配置)
  11. javascript的对象 和 JSON 对象?
  12. 学习笔记day1-计算机介绍
  13. PHP 网页支付支付宝支付接口对接
  14. 阿里云DataV数据过滤器取Value值大于等于25的数据(1)
  15. 进程管理(一)--进程管理的基本概念
  16. 【无线电】无线电频谱和波段划分
  17. Golang 函数耗时统计
  18. 部署无鱼工时系统,超详细教程,并成功部署
  19. Ubuntu 16.04系统安装VS Code流程详解
  20. 微信公众号关注页、微信关注链接 微信7.0 IOS 安卓

热门文章

  1. Qt随笔 - QSettings
  2. fatal error LNK1120: 11 unresolved externals
  3. request.setAttribute()用法
  4. sql cast()函数
  5. 使用友元,编译出错fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786) 的解决...
  6. Verilog中wire与reg类型的区别(转载自http://www.cnblogs.com/farbeyond/p/5204586.html)
  7. Linux下openSSL安装并颁发证书
  8. 安卓开发第一个小程序HelloWorld
  9. PathCopyCopy一键复制文件路径
  10. Springboot项目结构浅析