Output: Standard Output
Time Limit: 3 Seconds
Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular if and only if its sides are all parallel to the axis.
Input
The first line contains the number of tests t(1<=t<=10). Each case contains a single line with a positive integern(1<=n<=5000), the number of points. There are n lines follow, each line contains 2 integers x, y (0<=x, y<=109) indicating the coordinates of a point.
Output
For each test case, print the case number and a single integer, the number of regular rectangles found.
2 5 0 0 2 0 0 2 2 2 1 1 3 0 0 0 30 0 900 |
Case 1: 1 Case 2: 0 |
Problemsetter: Rujia Liu, Member of Elite Problemsetters' Panel
Special Thanks: IOI2003 China National Training Team
直接用map判断(n^2logn)超时了,改成先离散化再判断(n^2)可AC。
#include<cstdio> #include<map> #include<queue> #include<cstring> #include<iostream> #include<cstring> #include<algorithm> #include<vector> #include<stack> #include<cmath> using namespace std; const int maxn = 5000 + 5; const int INF = 1000000000; const double eps = 1e-6; typedef long long LL; typedef pair<int, int> P; P p[maxn]; int vis[maxn][maxn]; vector<int> vx, vy; int main(){ int t, kase = 0; scanf("%d", &t); while(t--){ kase++; int n; scanf("%d", &n); for(int i = 0;i < n;i++) scanf("%d%d", &p[i].first, &p[i].second); vx.clear();vy.clear(); for(int i = 0;i < n;i++){ vx.push_back(p[i].first); vy.push_back(p[i].second); } sort(vx.begin(), vx.end()); sort(vy.begin(), vy.end()); vx.erase(unique(vx.begin(), vx.end()), vx.end()); vy.erase(unique(vy.begin(), vy.end()), vy.end()); memset(vis, 0, sizeof vis); for(int i = 0;i < n;i++){ int x = find(vx.begin(), vx.end(), p[i].first)-vx.begin(); int y = find(vy.begin(), vy.end(), p[i].second)-vy.begin(); p[i].first = x; p[i].second = y; vis[x][y] = 1; } int ans = 0; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ int x0 = p[i].first; int y0 = p[i].second; int x1 = p[j].first; int y1 = p[j].second; if(x1!=x0 && y1!=y0 && vis[x1][y0] && vis[x0][y1]) ans++; } } printf("Case %d: %d\n", kase, ans/4); } return 0; }