博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA - 10574 Counting Rectangles
阅读量:6957 次
发布时间:2019-06-27

本文共 1919 字,大约阅读时间需要 6 分钟。

Description

Problem H

Counting Rectangles

Input: Standard Input

Output:Standard Output

Time Limit: 3Seconds

 

Given n points on the XY plane, count how many regular rectanglesare formed. A rectangle is regular if and only if its sides are all parallel tothe axis.

 

Input

Thefirst line contains the number of tests t(1<=t<=10).Each case contains a single line with a positive integer n(1<=n<=5000),the number of points. There are n lines follow, each line contains 2integers x, y (0<=x, y<=109)indicating the coordinates of a point.

 

Output

Foreach test case, print the case number and a single integer, the number ofregular rectangles found.

 

SampleInput                            Output for Sample Input

2

5

0 0

2 0

0 2

2 2

1 1

3

0 0

0 30

0 900

Case 1: 1

Case 2: 0

题意:给定平面上的n个点,统计它们能组成多少个边平行于坐标轴的矩形

思路:题目要求平行于坐标轴,那么我们先找同一x轴的两个点组成的线段,保存两个点的y轴坐标,那么我们仅仅要再找两个端点在y轴平行的这样就能找到矩形了

#include 
#include
#include
#include
using namespace std;typedef long long ll;const int maxn = 5010;struct Point { int x, y; bool operator< (const Point &a) const { if (x != a.x) return x < a.x; return y < a.y; }} p[maxn];struct Edge { int y1, y2; Edge() {} Edge(int y1, int y2) { this->y1 = y1; this->y2 = y2; } bool operator <(const Edge &a) const { if (y1 != a.y1) return y1 < a.y1; return y2 < a.y2; }} e[maxn*maxn];int n;int main() { int t; int cas = 1; scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y); sort(p, p+n); int num = 0; for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) { if (p[i].x != p[j].x) break; e[num++] = Edge(p[i].y, p[j].y); } sort(e, e+num); int tmp = 1, ans = 0; for (int i = 1; i < num; i++) { if (e[i].y1 == e[i-1].y1 && e[i].y2 == e[i-1].y2) tmp++; else { ans += tmp * (tmp-1) / 2; tmp = 1; } } ans += tmp * (tmp-1) / 2; printf("Case %d: %d\n", cas++, ans); } return 0;}

转载地址:http://ahlil.baihongyu.com/

你可能感兴趣的文章
小猿圈解析Linux系统启动过程
查看>>
4 种在 Linux 中检查默认网关或者路由器 IP 地址的方法
查看>>
spring cloud构建java版 b2b2c电子商务云商平台
查看>>
区块链100讲:Hyperledger Composer及其开发流程
查看>>
Weex收录
查看>>
undefind和null的区别
查看>>
其实微信小程序也没那么复杂
查看>>
体验下 Go 语言的魅力(初试)
查看>>
Docker 快速入门
查看>>
UIBarButtonItem 在 iOS 11 上的改变及应对方案
查看>>
再聊神经网络与深度学习
查看>>
OkHttp源码解析
查看>>
React全家桶+Egg 做一个协作聊天室~
查看>>
小程序从手动埋点到自动埋点
查看>>
vue-devtools的安装与使用
查看>>
iOS ARKit录制视频(AVAssetWriter & 有声音)
查看>>
### bootstrap-datepicker 与bootstrapValidator同时使用时,选择日期后,无法正常触发校验...
查看>>
2019最新前端面试宝典
查看>>
Puppeteer爬取网页数据
查看>>
flutter 语言篇四
查看>>