题目1 : Smallest Rectangle

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

You are given N 2D points P1, P2, ... PN. If there is a rectangle satisfying that its 4 vertices are in the given point set and its 4 sides are parallel to the axis we say the rectange exists.

Find the smallest exsisting rectange and output its area.

输入

The first line contains an integer N. (1 <= N <= 1000)

The following N lines each contain 2 integers Xi and Yi denoting a point (Xi, Yi). (0 <= Xi, Yi <= 1000000)

输出

Output the smallest area. If no rectangle exsists output -1.

样例输入

9
0 0
0 1
0 4
1 0
1 1
1 4
4 0
4 1
4 4

样例输出

1

把所有的点都放在哈希表里,这样可以O(1)判断一个坐标(x, y)上是不是有点。

然后枚举其中两个点,不妨设是(Xi, Yi)和(Xj, Yj),当作矩形的左上角顶点和右下角顶点。

这样我们只需判断(Xi, Yj)和(Xj, Yi)上有没有点即可判断是否构成一个矩形。如果构成矩形的化,更新当前最小面积。

总的时间复杂度是O(N^2)。

import java.util.*;
public class Main {static class Point implements Comparable<Point> {long x;long y;Point(long x, long y) {this.x = x;this.y = y;}public int hashCode(){return (int)((x << 20) + y); }public boolean equals(Object obj)  {Point p = (Point)obj;  return x == p.x && y == p.y;} public int compareTo(Point o) {if (x == o.x) {if (y == o.y) {return 0;} return y < o.y ? -1 : 1;}return x < o.x ? -1 : 1;}}public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();HashSet<Point> set = new HashSet<>();ArrayList<Point> arr = new ArrayList<>();for(int i = 0; i < n; i++) {long x = sc.nextLong();long y = sc.nextLong();arr.add(new Point(x, y));set.add(new Point(x, y));}Long ans = -1L;for (int i = 0; i < n; i++) {for (int j = i + 1; j < n; j++) {long x1 = arr.get(i).x;long y1 = arr.get(i).y;long x2 = arr.get(j).x;long y2 = arr.get(j).y;if (x1 == x2 || y1 == y2) {continue;}if(set.contains(new Point(x1, y2)) && set.contains(new Point(x2, y1))) {long area = Math.abs((x1 - x2) * (y1 - y2));if (ans == -1L || area < ans) {ans = area;}}}}System.out.println(ans);}
}

Smallest Rectangle相关推荐

  1. 6 最小覆盖矩形(Smallest Rectangle Enclosing Black Pixels)

    文章目录 1 题目 2 描述 3 思路 3.1 图解 3.2 时间复杂度 3.3 空间复杂度 4 源码 1 题目   最小覆盖矩形(Smallest Rectangle Enclosing Black ...

  2. graythresh matlab,Matlab-图形算法和图像处理指南

    笔者前言 本指南一共9 章,由 Valentina Plekhanova 博士撰写,链接地址http://osiris.sunderland.ac.uk/%7Ecs0vpl/GIP-VP%20Tuto ...

  3. matlab greythresh,Matlab-图形算法和图像处理指南

    笔者前言 本指南一共9 章,由 Valentina Plekhanova 博士撰写,链接地址http://osiris.sunderland.ac.uk/%7Ecs0vpl/GIP-VP%20Tuto ...

  4. 高级UI-画笔Paint

    在UI这一块,谈到自定义,就离不开画笔和画布的使用话题,在自定义控件的时候,为了做出炫酷的效果,我们往往会使用画笔和画布,那么这里我们就先来看看画笔的使用吧 简单使用例子 自定义一个View publ ...

  5. Halcon: 畸变矫正与标定(1)

    1. Halcon相机标定和图像矫正     对于相机采集的图片,会由于相机本身和透镜的影响产生形变,通常需要对相机进行标定,获取相机的内参或内外参,然后矫正其畸变.相机畸变主要分为径向畸变和切向畸变 ...

  6. halcon相机标定及图像矫正(代码)

    侵删 1 halcon相机标定和图像矫正 对于相机采集的图片,会由于相机本身和透镜的影响产生形变,通常需要对相机进行标定,获取相机的内参或内外参,然后矫正其畸变.相机畸变主要分为径向畸变和切向畸变,其 ...

  7. halcon/c++接口基础 之 控制参数

    HALCON/C++可以处理各种不同类型的字母数字混合的控制参数,如下: 离散数字(long) 浮点数字(double) 字符串(char*) 控制参数的一个特殊形式是句柄,提供了途径去访问复杂的数据 ...

  8. 【HDU - 1937 】Finding Seats(二维前缀和+尺取法)

    题干: A group of K friends is going to see a movie. However, they are too late to get good tickets, so ...

  9. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

最新文章

  1. 【vue插件篇】vue-form-check 表单验证
  2. 自动化控制之线程池的使用
  3. linux命令总结之traceroute命令
  4. Java程序员从笨鸟到菜鸟之(九十九)深入java虚拟机(八)开发自己的类加载器...
  5. C编译中如何向代码中传递一个预定义字串
  6. 用计算机破解vivo手机数字密码,vivo手机咋样在电脑上解锁?看vivoy66解锁密码找答案。...
  7. win7美化_Windows桌面图标和分类美化小工具
  8. 补充:消除epsilon产生式等
  9. C++ opencv之像素操作之逻辑操作(bitwise_and,bitwise_xor,bitwise_or,bitwise_not)
  10. matlab作业1,matlab作业报告一
  11. GMAC接口(2)——协议
  12. 三跨考研浙江大学计算机,“三跨”考研的焦虑 你能承受多少
  13. java 自省_javabean的自省机制
  14. vue在初始化时给scrollTop设置一个值,但scrollTop却始终为0
  15. 灵魂显示正在登录聊天服务器,Soul跟随灵魂找到你
  16. IQ正交调制及星座图
  17. 移动端扫描vin码(车架号)识别
  18. jsp城市智能公交查询系统
  19. 网站后台如何判断客户端是来自手机、还是来自电脑?
  20. 电力英语计算机教材,实用科技英语(21世纪高等教育计算机规划教材)

热门文章

  1. Led显示屏是怎样散热的?
  2. 组件分享之前端组件——初学者的web开发课程Web-Dev-For-Beginners
  3. 奥格斯堡大学计算机系,奥格斯堡大学排名
  4. 阿里旺旺2012聊天获取(买家版)
  5. HDU - 5852 Intersection is not allowed! LGV定理
  6. 5 查找单价最高的书
  7. 射频电路layout 总结
  8. 个人简历(markdown)
  9. 很老的文章:Mapbar:地图搜索的人民战争
  10. asuswrt 单臂路由_我的软路由折腾-斐讯N1单臂路由设置