Description
Problem H Counting rectangles
Input: Standard Input
Output:Standard output
Time limit:3 seconds
GivenNPoints onXYPlane, count how many regular rectanglesare formed. A rectangle is regular if and only if its sides are all parallel tothe axis.
Input
Thefirst line contains the number of testsT (1 <= T <= 10). Each case contains a single line with a positive integerN(1<=N<=5000), The number of points. There areNLines follow, each line contains2IntegersX,Y(0<=X,Y<=109) Indicating the coordinates of a point.
Output
Foreach test case, print the case number and a single integer, the number ofregular rectangles found.
sampleinput output for sample input
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 |
N points on the plane are given, and the number of edges in the coordinate axis is counted.
Idea: the subject must be parallel to the coordinate axis. First, we need to find a line segment composed of two points on the same X axis and save the Y axis coordinates of the two points, then we only need to find two endpoints parallel to the Y axis to find the rectangle.
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> using namespace STD; typedef long ll; const int maxn = 5010; struct point {int X, Y; bool operator <(const point & A) const {If (X! =. X) return x <. x; return Y <. Y ;}} P [maxn]; struct edge {int Y1, Y2; edge () {}edge (INT Y1, int Y2) {This-> Y1 = Y1; this-> Y2 = Y2;} bool operator <(const edge & A) const {If (Y1! =. Y1) return Y1 <. y1; return Y2 <. y2 ;}} E [maxn * maxn]; int N; int main () {int t; int CAS = 1; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); For (INT I = 0; I <n; I ++) scanf ("% d", & P [I]. x, & P [I]. y); sort (p, p + n); int num = 0; For (INT I = 0; I <n; I ++) for (Int J = I + 1; j <n; j ++) {If (P [I]. x! = P [J]. x) break; E [num ++] = edge (P [I]. y, P [J]. y);} Sort (E, E + num); int TMP = 1, ANS = 0; For (INT I = 1; I <num; I ++) {If (E [I]. y1 = E [I-1]. y1 & E [I]. y2 = E [I-1]. y2) TMP ++; else {ans + = TMP * (tmp-1)/2; TMP = 1;} ans + = TMP * (tmp-1)/2; printf ("case % d: % d \ n", CAS ++, ANS);} return 0 ;}