UVA 10574 - Counting Rectangles(枚举+计数)
2014-05-24 00:00
330 查看
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 ; 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(计数)
- uva 294 - Divisors(枚举+计数)
- uva 10574 - Counting Rectangles(计数)
- uva 1045 最佳完美匹配 km算法 && 枚举
- uva 216 Getting in Line (暴力枚举)
- 第一章例题7偶数矩阵UVa 11464(枚举)
- UVA 1510 Neon Sign(计数)
- UVa 1508 - Equipment (状态压缩 + 枚举子集)
- 枚举,预处理,条件概率(条件概率,uva 11181)
- UVA 10012 How Big Is It?(暴力枚举)
- UVa - 11059 - Maximum Product(枚举)
- [枚举最小瓶颈生成树]UVa-1395 - Slim Span(kruskal)
- UVA 11889 - Benefit 可直接枚举
- uva 1352 - Colored Cubes(枚举)
- UVA - 131 The Psychic Poker Player (暴力枚举+模拟)
- UVa229 - Scanner(枚举技巧)
- Uva 10766 Organising the Organisation (Matrix_tree 生成树计数)
- uva 11464 - Even Parity(暴力枚举)
- 例题7-1 UVa725 Division(枚举)