六、完善程序-2

(2) (矩形计数) 平面上有n 个关键点,求有多少个四条边都和 x 轴或者y 轴平行的矩形,满足四个顶点都是关键点。给出的关键点可能有重复,但完全重合的矩形只计一次。
试补全枚举算法。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>

struct point {
    int x, y, id;
};

bool equals(struct point a, struct point b) {
    return a.x == b.x && a.y == b.y;
}

bool cmp(struct point a, struct point b) {
    return ;
}

void sort(struct point A[], int n) {
    for (int i = 0; i < n; i++)
        for (int j = 1; j < n; j++)
            if (cmp(A[j], A[j - 1])) {
                struct point t = A[j];
                A[j] = A[j - 1];
                A[j - 1] = t;
            }
}
int unique(struct point A[], int n) {
    int t = 0;
    for(int i = 0; i < n; i++)
        if ()
            A[t++] = A[i];
    return t;
}

bool binary_search(struct point A[], int n, int x, int y) {
    struct point p;
    p.x = x;
    p.y = y;
    p.id = n;
    int a = 0,b = n - 1;
    while (a < b) {
        int mid =;
        if ()
            a = mid + 1;
        else
            b = mid;
    }
    return equals(A[a], p);
}
#define MAXN 1000
struct point A[MAXN];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &A[i].x, &A[i].y);
        A[i].id = i;
    }
    sort(A, n);
    n = unique(A, n);
    int ans = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if ( && binary_search(A, n, A[i].x, A[j].y) && binary_search(A, n, A[j].x, A[i].y)) {
                ans++;
            }
    printf("%d", ans);
    return 0;
}
Scroll to Top