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;
}

最新文章

  1. jquery插件treetable使用
  2. 黑马----JAVA比较器:Comparable和Comparator
  3. 在共享DLL中使用MFC
  4. 关于Expression表达式树的拼接
  5. WPF基础学习第二天(高级控件)
  6. HDOJ/HDU 1321 Reverse Text(倒序输出~)
  7. Linux下编译安装qemu和libvirt
  8. Eclipse代码自动提示设置
  9. dubbo服务简单搭建
  10. MySQL 排序
  11. C#语法——泛型的多种应用
  12. Kafka Tuning Recommendations
  13. 9. Web browser-related (网页浏览器相关 4个)
  14. Linux 互斥锁
  15. Google - chanceToLose24Game
  16. SQL Server 数据类型映射(转载)
  17. SQL使用技巧
  18. &lt;9&gt;cc.Sprite组件
  19. cmd 查看本地端口被占用程序
  20. CAShapeLayer的使用[1]

热门文章

  1. java学习之IO装饰设计模式
  2. hdu 1232 畅通project
  3. Swift--基本数据类型(一)
  4. Eucalyptus和Openstack最近版本的改动简单对比
  5. Python类的继承演示样例
  6. 写了交互给后台后来不能用?bug多多多又找不到文件效率低?工作流程帮你优化起来~~~~
  7. BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )
  8. 复习C语言系列二:动态调用函数指针数组
  9. Raphael入门实例:动画与箭头
  10. 神奇的矩阵 NOI模拟题