1 条题解

  • 0
    @ 2024-8-2 16:19:45

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    typedef long long LL;
    
    const int N = 1e5 + 10;
    
    struct Node {
        LL x, y;  // 顶点坐标
        LL l, r;  // 底的左侧和右侧
    }a[N];
    
    LL n,s;
    
    // 按照底的左侧排序
    bool cmp(Node na, Node nb) {
        return na.l < nb.l;
    }
    
    int main() {
        int n;
        cin >> n;
    
        for (int i = 1; i <= n; i++) {
            cin >> a[i].x >> a[i].y;
            a[i].l = a[i].x - a[i].y;
            a[i].r = a[i].x + a[i].y;
        }
    
        sort(a + 1, a + n + 1, cmp);
    
        LL s = a[1].y * a[1].y * 4;  // 防止出现小数乘以4
    
        int last = 1;  // 上一个使面积增加的三角形
    
        for (int i = 2; i <= n; i++) {
            if (a[i].l >= a[last].r) {
                // 情况 1:没有交集
                s += a[i].y * a[i].y * 4;
                last = i;
            } else if (a[i].l < a[last].r && a[i].r > a[last].r) {
                // 情况 2:有交集
                LL c = (a[last].r-a[i].l);  // 重叠部分的三角形底边
                s +=a[i].y*a[i].y*4-c*c;
                last = i;
            }
            // 情况 3:被覆盖面积未增加,不用修改 last
        }
    
        cout << s << endl;
    
        return 0;
    }
    
    
    • 1

    信息

    ID
    1016
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    2
    已通过
    2
    上传者