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 <algorithm>
using namespace std;
const int N = 5005;
int t, n;
struct Point {
int x, y;
void read() {
scanf("%d%d", &x, &y);
}
bool operator < (const Point a) const {
if (x != a.x)
return x < a.x;
return y < a.y;
}
} p[N];
struct Edge {
int y1, y2;
Edge(int y1 = 0, int y2 = 0) {
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[N * N / 2];
int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
p[i].read();
sort(p, p + n);
int en = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[j].x != p[i].x) break;
e[en++] = Edge(p[i].y, p[j].y);
}
}
sort(e, e + en);
long long cnt = 1, ans = 0;
for (int i = 1; i < en; i++) {
if (e[i].y1 == e[i - 1].y1 && e[i].y2 == e[i - 1].y2)
cnt++;
else {
ans += cnt * (cnt - 1) / 2;
cnt = 1;
}
}
ans += cnt * (cnt - 1) / 2;
printf("Case %d: %lld\n", ++cas, ans);
}
return 0;
}
UVA 10574 - Counting Rectangles(枚举+计数)的更多相关文章
- UVA - 10574 Counting Rectangles
Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...
- UVA 10574 - Counting Rectangles 计数
Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular ...
- Codeforces 372 B. Counting Rectangles is Fun
$ >Codeforces \space 372 B. Counting Rectangles is Fun<$ 题目大意 : 给出一个 \(n \times m\) 的 \(01\) ...
- 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 ...
- Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 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 ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
随机推荐
- Windows Azure 社区新闻综述(#75 版)
欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure 的社区推动新闻.内容和对话.以下是本周的亮点. 文章.视频和博客文章 · PowerShell 对 Windows A ...
- Sicily-1009 梅森素数
一.梅森素数 素数有无穷多个,却只有极少量的素数能表示成2p-1(p为素数)的形式.在不大于257的素数中,当p=2.3.5.7.13.17.19.31.67.127.257时,2p-1是素数,其它都 ...
- Java学习之国际化程序
国际化程序就是把程序的语言根据用户使用的语言显示,各个国家的用户都可以看懂 实现方法就是把输出文字都写在配置文件里,然后根据用户系统语言选择不同的语言输出 package com.gh; import ...
- codility上的练习 (1)
codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...
- extjs 优化小建议
1 原文信息 原文标题: Sencha Con 2013: Ext JS Performance tips 原文地址: [http://edspencer.net/2013/07/19/sencha- ...
- linux内核代码container_of
它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针. typedef unsigned int __kernel_size_t; typedef __ke ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- windows和centos用cutycapt截网页的图
centos下:(主要参考http://loosky.net/2816.html) (1)安装qt47 增加qt47的源 vim /etc/yum.repos.d/atrpms.repo //加入如下 ...
- 下载并在Eclipse中关联Android源代码
大家都知道文档写的好当然让人非常舒服,可是有时候文档再好也不如直接看源代码来的直接,既然Android是开源的,为什么不在eclipse里直接看它的源代码呢? 1.下载源代码 这部分网上有大量的资料, ...
- ORACLE列值合併
合併列值最通用的方法就是寫一個自定義函數去實現,這裏介紹的是其它方法. 在SQL Server中合併列值能够使用For Xml Path,在Oracle中則能够使用wm_concat 或 ListAg ...