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的更多相关文章
- UVA 10574 - Counting Rectangles(枚举+计数)
10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...
- UVA 10574 - Counting Rectangles 计数
Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular ...
- Counting Rectangles
Counting Rectangles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1043 Accepted: 546 De ...
- Project Euler 85 :Counting rectangles 数长方形
Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 co ...
- 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 ...
- Codeforces 372 B. Counting Rectangles is Fun
$ >Codeforces \space 372 B. Counting Rectangles is Fun<$ 题目大意 : 给出一个 \(n \times m\) 的 \(01\) ...
- uva 1436 - Counting heaps(算)
题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,如今为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一仅仅 ...
- UVA 12075 - Counting Triangles(容斥原理计数)
题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...
- [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)
Description We are given a figure consisting of only horizontal and vertical line segments. Our goal ...
随机推荐
- 谷歌Cartographer学习(1)-快速安装测试(转载)
转载自谷歌Cartographer学习(1)-快速安装测试 代码放到个人github上,https://github.com/hitcm/ 如下,需要安装3个软件包,ceres solver.cart ...
- js埋点(转载)
页面埋点的作用,其实就是用于流量分析.而流量的意思,包含了很多:页面浏览数(PV).独立访问者数量(UV).IP.页面停留时间.页面操作时间.页面访问次数.按钮点击次数.文件下载次数等.而流量分析又有 ...
- 银行综合储蓄业务系统,水平为学了一年C语言
银行综合储蓄业务系统 #include <stdio.h> #include<string.h> int acccunt = 0; char name[10],pw[10]; ...
- Jquery ui datepicker 设置日期范围,如只能隔3天
最近的后台项目前端使用了jquery ui 日历控件自然就使用了jquery ui 的 datepicker 后台数据比较好大,一般是千万级的和百万级的关联,查询会很慢,所以后加想多加些过滤条件, ...
- C#自定义控件在添加引用后不显示在工具箱的解决方法
先说一些背景: 在开发C#项目时,发现很多控件存在复用的情况,控件的属性都是要设置成一样的,我就想,能不能设置一个类来存放这个控件,这样我每次用的时候直接加一些特殊的操作就可以了,不需要再次设置控件属 ...
- Reverse Integer (JAVA)
public class Solution { public int reverse(int x) { StringBuffer sb = new StringBuffer(x+"" ...
- Arrays.copyof
public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System. ...
- win7系统64位安装oracle10g
win7系统64位安装oracle10g 下载地址: http://download.oracle.com/otn/nt/oracle10g/10204/10204_vista_w2k8_x64_pr ...
- HDU 1055 - Color a Tree
一棵树,结点树为n,根结点为r.每个结点都有一个权值ci,开始时间为0,每染色一个结点需要耗时1,每个结点的染色代价为ci*ti(ti为当前的时间),每个结点只有在父结点已经被染色的条件下才能被染色. ...
- C++标准库之泛型算法
本文中算法都是指泛型算法. 基本要点: 1)算法使用迭代器进行操作. 2)不依赖容器,但容器希望使用算法,就必须提供接口. 3)通用算法永远不会执行容器操作.操作仅指:更改容器大小的操作.但,容器内部 ...