UVA - 10574 Counting Rectangles

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

时间: 2024-10-13 04:59:45

UVA - 10574 Counting Rectangles的相关文章

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

10574 - Counting Rectangles 题目链接 题意:给定一些点,求能够成几个矩形 思路:先把点按x排序,再按y排序,然后用O(n^2)的方法找出每条垂直x轴的边,保存这些边两点的y坐标y1, y2.之后把这些边按y1排序,再按y2排序,用O(n)的方法找出有几个连续的y1, y2都相等,那么这些边两两是能构成矩形的,为C2cnt种,然后累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <

UVA 10574 - Counting Rectangles 计数

Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular if and only if its sides are all parallel to the axis.InputThe ?rst line contains the number of tests t (1 ≤ t ≤ 10). Each case contains a single lin

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

Project Euler 85 :Counting rectangles 数长方形

Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles: Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the

ZOJ (狗狗)1426 Counting Rectangles(暴力)

Counting Rectangles Time Limit: 2 Seconds      Memory Limit: 65536 KB We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example,

uva 12075 - Counting Triangles(容斥原理)

题目链接:uva 12075 - Counting Triangles 题目大意:一个n?m的矩阵,求说有选任意三点,可以组成多少个三角形. 解题思路:任意选三点C(3(n+1)?(m+1))但是有些组合是不可行得,即为三点共线,除了水平和竖直上的组合,就是斜线上的了,dp[i][j]即为ij情况下的斜线三点共线. #include <cstdio> #include <cstring> typedef long long ll; const int N = 1005; ll dp

uva 1436 - Counting heaps(计数)

题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,现在为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一只的uva11174,最后问题只和树德结构有直接关系,f(root)=(s(root)?1)!(s(1)?s(2)???s(n) 但是给定的取模数不是质数,所以不能用逆元做,只能将分子分母分别拆分成质因子,然后对质因子进制约分.因为最后的答案一定是正整数,所以对于每个质因子,分子分解出的因子个数一定大于

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

题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对于两点,求他们的gcd - 1,得到的就是他们之间有多少个点,那么情况数就可以求了,然后还是利用容斥原理去计数,然后累加出答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using nam

UVA 1436 - Counting heaps(计数问题)

UVA 1436 - Counting heaps 题目链接 题意:给定一个树的结构,放1-n数字进去,父亲结点值必须小于子节点,问情况有几种. 思路:f[u]表示以u为子树的情况,那么子树情况为f(v1), f(v2), f(v3)... f(vn).去组成子树相当于从中选s(v1), s(v2), s(v3) ... s(vn).根据组合数学,情况为f(v1) f(v2) ... f(vn) (s(u) - 1)! \ (s(v1)! s(v2)! ... * s(vn)!)化简后得到公式: