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晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
随机推荐
- POJ-1003&1004
这两题比较简单,就不做分析了,描述下题目,就上代码吧. [题目描述] 1003,其实就是求这个方程的最小n:1/2 + 1/3 + 1/4 + ... + 1/(n + 1) >= c: 100 ...
- objective-III 窗口应用程序
objective-III 一.创建窗口应用程序 打开xcode->create->在iso目录下选择empty-null->创建 在打开的项目文件名上右击NEW FILE,在io ...
- cocos2D(二)---- cocos2D文档的使用
在使用cocos2d进行游戏开发的过程中,难免要查阅cocos2d的API文档.搞清楚怎么使用某个类或者某个方法.幸运的是,cocos2d的作者已经在源码里面加入了文档凝视,我们仅仅须要使用文档生成工 ...
- BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )
题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...
- 枚举最短路径+SPFA
Harry Potter and the Final Battle Submit Status Description The final battle is coming. Now Harry Po ...
- 转:CI引入外部js与css
其实不管是在用CI还是ZF都有同样一个问题,就是路径的问题.前期,我在用ZF做CMS时,我在.htaccess文件中设置了如遇到js,css,img等资源文件都不重定向.但今天在用CI时,却忘记了,搞 ...
- Mojo 自动发布接口
get '/api/auto_publish/publish' => sub{ my $c = shift; my $env = $c->param('env'); my $app = $ ...
- 《UNIX环境高级编程》笔记--sync、fsync和fdatasync函数
传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘 I/O都通过缓冲进行.当将数据写入文件时,内核通常先将该数据复制到其中一个缓冲区中,如果该缓冲区尚未写满,则并不将其排入输出队列 ...
- Linux内核源代码解析之——我与神童聊Linux内核
本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/11619609 我的朋友里,至少有2.5个神童. 有的 ...
- java--线程的睡眠sleep()
package MyTest; public class Demo1 extends Thread { public void run() { loop(); } public void loop() ...