uva 10574 - Counting Rectangles(计数)
2014-05-29 13:03
239 查看
题目链接:uva 10574 - Counting Rectangles
题目大意:给出n个点,问选出4个点作为定点,可以组成多少个平行与坐标轴的矩形。
解题思路:首先将点按照x排序(优化),然后处理出所有平行于y轴的线段,记录这些线段的y1和y2,接着只要找出y1和y2值均相等的边,C(2cnt).
题目大意:给出n个点,问选出4个点作为定点,可以组成多少个平行与坐标轴的矩形。
解题思路:首先将点按照x排序(优化),然后处理出所有平行于y轴的线段,记录这些线段的y1和y2,接着只要找出y1和y2值均相等的边,C(2cnt).
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 5005; struct point { int x, y; void get () { scanf("%d%d", &x, &y); } void set (int x, int y) { this->x = x; this->y = y; } }p , l[N*N/2]; int n, m; inline bool cmp(const point& a, const point& b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; } void init () { scanf("%d", &n); for (int i = 0; i < n; i++) p[i].get(); sort(p, p + n, cmp); m = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { if (p[i].x != p[j].x) break; l[m++].set(p[i].y, p[j].y); } } sort(l, l + m, cmp); } ll solve () { ll ans = 0; for (int i = 0; i < m;) { int mv = i+1; while (l[i].x == l[mv].x && l[i].y == l[mv].y) mv++; ll c = mv - i; if (c >= 2) ans += c * (c - 1) / 2; i = mv; } return ans; } int main () { int cas; scanf("%d", &cas); for (int i = 1; i <= cas; i++) { init(); printf("Case %d: %lld\n", i, solve()); } return 0; }
相关文章推荐
- uva 10574 - Counting Rectangles(计数)
- UVA 10574 - Counting Rectangles(枚举+计数)
- UVA 10574 - Counting Rectangles(枚举+计数)
- 递推,计数(危险的组合,uva 580)
- UVALive 3720 Highways(组合计数、递推)
- Uva 1510 - Neon Sign 解题报告(计数)
- UVA 12075 - Counting Triangles(容斥原理计数)
- UVA - 10574 Counting Rectangles
- uva 278 - Chess(计数问题)
- uva 10616 - Divisible Group Sums(计数)
- UVA 11645 - Bits(数论+计数问题)
- uva 11038 - How Many O's?(计数问题)
- uva 11481 - Arrange the Numbers(计数问题)
- UVa 10574 - Counting Rectangles
- uva11404(数论_三角形计数)
- 训练指南-2.1-计数-uva11401数三角形-递推
- UVA-11806 Cheerleaders 计数问题 容斥定理
- 【基本计数方法---加法原理和乘法原理】UVa 11538 - Chess Queen
- UVA 1264 - Binary Search Tree(BST+计数)
- uva 12034 - Race(dp计数)