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. 【转】android 电容屏(二):驱动调试之基本概念篇

    关键词:android  电容屏 tp 工作队列 中断 多点触摸协议平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV310(samsung ...

  2. 单调队列-hdu-3415-Max Sum of Max-K-sub-sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3415 题目大意: 给n个数凑成环状,求某一区间,使得该区间长度不超过k,且总和最大. 解题思路: 区 ...

  3. CF 338 D GCD Table(CRT)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 给定一个序列,a[1 ..k],问是否存在(i , ...

  4. 杭电 3887 Counting Offspring

    根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])- ...

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

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

  6. MySQL中explain的type类型

    |  ALL              |  全表扫描 |  index            |  索引全扫描 |  range            |  索引范围扫描,常用语<,<= ...

  7. java学习之Date的使用

    Date使用,主要要注意将日期格式化,否则返回的是系统默认的格式.请自己查阅API文档. import java.util.*; import java.text.*; public class Te ...

  8. ios9基础知识(技能篇)

    NSFileManager.NSURL.NSFileHandle.NSData.NSXMLParser.NSUserDefaults.NSKeyedArchiver.NSKeyedUnarchiver ...

  9. C#中的多线程使用 -- Thread 类详解(转)

    现在C#已经建议摈弃使用 Suspend, Resume 暂停/恢复线程, 也尽量少用 Abort方法中断一个线程. 建议使用线程的同步手段有: Mutex.ManualResetEvent.Auto ...

  10. Hibernate注解学习1

    由于项目的原因目前需要一些简单注解,于是就做了个hibernate注解的小demo,来记录一下. 1.需要引入jar包ejb3-persistence.jarhibernate-annotations ...