POJ - 1971 Parallelogram Counting

There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4-element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line.

Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases. It is followed by the input data for each test case. 
The first line of each test case contains an integer n (1 <= n <= 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000. 

Output

Output should contain t lines. 
Line i contains an integer showing the number of the parallelograms as described above for test case i. 

Sample Input

2
6
0 0
2 0
4 0
1 1
3 1
5 1
7
-2 -1
8 9
5 7
1 1
4 8
2 0
9 8

Sample Output

5
6

求出最多有几个平行四边形
两条线段中点相同就是平行四边形

我一开始是暴力做的 ,正在学哈希 就用哈希写一下

暴力
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <string>
 7 #include<sstream>
 8 #include<set>
 9 #include <cstdlib>
10 #include<map>
11 using namespace std;
12 const int maxn = 1e6 + 10;
13 struct node {
14     double x, y;
15     node() {}
16     node(double x, double y): x(x), y(y) {}
17 } a[1010], b[maxn];
18 int cmp(node a, node b) {
19     if (a.x == b.x) return a.y < b.y;
20     return a.x < b.x;
21 }
22 int main() {
23     int t;
24     scanf("%d", &t);
25     while(t--) {
26         int n;
27         scanf("%d", &n);
28         memset(a, 0, sizeof(a));
29         memset(b, 0, sizeof(b));
30         for (int i = 0 ; i < n ; i++ )
31             scanf("%lf%lf", &a[i].x, &a[i].y);
32         int k = 0;
33         for (int i = 0 ; i < n - 1 ; i++) {
34             for (int j = i + 1 ; j < n ; j++) {
35                 b[k++] = node((a[i].x + a[j].x) / 2, (a[i].y + a[j].y) / 2);
36             }
37         }
38         sort(b, b + k, cmp);
39         int ans = 1, sum = 0;
40         for (int i = 0 ; i < k - 1 ; i++) {
41             if (b[i].x == b[i + 1].x && b[i].y == b[i + 1].y ) ans++;
42             else {
43                 sum += ans * (ans - 1) / 2;
44                 ans = 1;
45             }
46         }
47         printf("%d\n", sum);
48     }
49     return 0;
50 }

哈希

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <set>
#include <cstdlib>
#include <map>
using namespace std;
const int maxn = 1e6 + 3;
struct node {
    int x, y, next, sum;
} a[maxn + 10];
int head[maxn], x[1010], y[1010], tot;

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        tot = 0;
        memset(head, -1, sizeof(head));
        memset(a, 0, sizeof(a));
        int n;
        scanf("%d", &n);
        int ans = 0;
        for (int i = 0 ; i < n ; i++) {
            int x1, y1;
            scanf("%d%d", &x[i], &y[i]);
            for (int j = 0 ; j < i ; j++) {
                int flag = 1;
                int nx = x[i] + x[j];
                int ny = y[i] + y[j];
                int h = abs(nx + ny) % maxn;
                for (int k = head[h] ; ~k ; k = a[k].next) {
                    if (a[k].x == nx && a[k].y == ny ) {
                        a[k].sum++;
                        ans += a[k].sum;
                        flag = 0;
                        break;
                    }
                }
                if (flag) {
                    a[tot].x = nx;
                    a[tot].y = ny;
                    a[tot].next = head[h];
                    head[h] = tot++;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


猜你喜欢

转载自www.cnblogs.com/qldabiaoge/p/9159651.html
今日推荐