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 <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
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;
}

UVA - 10574 Counting Rectangles的更多相关文章

  1. UVA 10574 - Counting Rectangles(枚举+计数)

    10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...

  2. UVA 10574 - Counting Rectangles 计数

    Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular ...

  3. Counting Rectangles

    Counting Rectangles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1043 Accepted: 546 De ...

  4. Project Euler 85 :Counting rectangles 数长方形

    Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 co ...

  5. Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和

    D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...

  6. Codeforces 372 B. Counting Rectangles is Fun

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

  7. uva 1436 - Counting heaps(算)

    题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,如今为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一仅仅 ...

  8. UVA 12075 - Counting Triangles(容斥原理计数)

    题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...

  9. [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)

    Description We are given a figure consisting of only horizontal and vertical line segments. Our goal ...

随机推荐

  1. 蚂蚁的难题(二)首尾相连数组的最大子数组和(DP)

    蚂蚁的难题(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 下雨了,下雨了,蚂蚁搬家了. 已知有n种食材需要搬走,这些食材从1到n依次排成了一个圈.小蚂蚁对每种食材 ...

  2. C#.Net前台线程与后台线程的区别

    本文来自:http://www.cnblogs.com/zfanlong1314/archive/2012/02/26/2390455.html .Net的公用语言运行时(Common Languag ...

  3. 一起talk GDB吧(第七回:GDB监视功能)

    各位看官们.大家好.上一回中我们说的是GDB改动程序执行环境的功能.而且说了怎样使用GDB改动变量 的值.这一回中.我们继续介绍GDB的调试功能:监视功能.当然了,我们也会介绍怎样使用GDB的监视功 ...

  4. [有向图的强连通分量][Tarjan算法]

    https://www.byvoid.com/blog/scc-tarjan 主要思想 Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树.搜索时,把当前搜索树中未处理的 ...

  5. android——生成或者下载的图片在相册中找不到

    今天在写程序的时候,遇到了一个问题,就是生成的图片一直都不能在相册中显示出来,而且,就连通过发送Intent过去,都找不到.通过在网上搜索,发现了一个很好的方法. Intent intent = ne ...

  6. ASP.NET 开发框架汇总

    先简单记录一下,以后慢慢添加 1.ASP.NET Aries 2.ASP.NET DevExpress

  7. 链接<a href="javascript:void(0)" />

    1.<a href="#"></a>——点击链接后,页面会向上滚到页首,# 默认锚点为 #TOP 2.<a href="javascript ...

  8. SharePoint各版本信息

    参考网页http://blogs.msdn.com/b/erica/archive/2013/05/30/sharepoint-server-2010-version-reference.aspx

  9. Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...

  10. Java中的static关键字

    写在前面: 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中static表示“全局”或“静态”的意思,用来修饰成员变量和成员方法,当然也可以修饰 ...