1 条题解

  • 1
    @ 2025-3-31 20:41:27

    题意

    根据给定的垂直和水平的线段,求能组成的矩形有多少个。

    题解

    因为题目给的只有垂直和水平的线段,且总线段不超过100100.所以我们可以暴力。 11、任选两根水平的线段,若无水平线段可选,结束。 22、从所有的垂直线段里,找到和这两根水平线段相交的线段,假设有tmptmp条。 33、对于11步选的两条水平线段,因为有tmptmp跟垂直线段与其相交,根据推算,可以得知,其能组成的矩形就是tmp1tmp/2(tmp - 1)*tmp / 2 个,将其加进总和里即可

    标程

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    #define MAX_ 101
    typedef long long LL;
    
    struct line {
        int x[2];
        int y[2];
    };
    
    int ans;
    
    int main() {
        int Case, n, h, v, tmp, maxh, minh, maxv, minv;
        int x1, y1, x2, y2;
    
        std::scanf("%d", &Case);
        while (Case--) {
            std::scanf("%d", &n);
            ans = 0;
            h = v = 0;
    
            struct line hor[MAX_];
            struct line ver[MAX_];
    
            for (int i = 0; i < n; i++) {
                std::scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
                if (y1 == y2) {
                    if (x1 < x2) {
                        hor[h].x[0] = x1;
                        hor[h].x[1] = x2;
                    } else {
                        hor[h].x[0] = x2;
                        hor[h].x[1] = x1;
                    }
                    hor[h].y[0] = y1;
                    hor[h].y[1] = y2;
                    h++;
                } else {
                    if (y1 < y2) {
                        ver[v].y[0] = y1;
                        ver[v].y[1] = y2;
                    } else {
                        ver[v].y[0] = y2;
                        ver[v].y[1] = y1;
                    }
                    ver[v].x[0] = x1;
                    ver[v].x[1] = x2;
                    v++;
                }
            }
    
            for (int i = 0; i < h - 1; i++) {
                for (int j = i + 1; j < h; j++) {
                    if (hor[i].x[1] < hor[j].x[0] || hor[i].y[0] == hor[j].y[0]) {
                        continue;
                    }
                    if (hor[i].x[1] < hor[j].x[1]) {
                        maxh = hor[i].x[1];
                    } else {
                        maxh = hor[j].x[1];
                    }
                    if (hor[i].x[0] > hor[j].x[0]) {
                        minh = hor[i].x[0];
                    } else {
                        minh = hor[j].x[0];
                    }
                    if (hor[i].y[0] > hor[j].y[0]) {
                        maxv = hor[i].y[0];
                    } else {
                        maxv = hor[j].y[0];
                    }
                    if (hor[i].y[0] < hor[j].y[0]) {
                        minv = hor[i].y[0];
                    } else {
                        minv = hor[j].y[0];
                    }
                    tmp = 0;
                    for (int k = 0; k < v; k++) {
                        if (ver[k].x[0] >= minh && ver[k].x[0] <= maxh && ver[k].y[0] <= minv && ver[k].y[1] >= maxv) {
                            tmp++;
                        }
                    }
                    ans += (tmp - 1) * tmp / 2;
                }
            }
            std::printf("%d\n", ans);
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    694
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者