usaco5.5.1 Picture

本文介绍了一个计算多个矩形海报粘贴在一起形成的区域周长的方法。通过将问题分解为针对X轴和Y轴方向上的边界计算,利用排序和计数数组的技术来确定哪些线段最终构成了整个图形的边界。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 原题

Picture
IOI 1998

A number, N (1 <= N < 5000), of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. Write a program to calculate the perimeter.

Figure 1 shows an example with seven rectangles: 

Figure 1. A set of seven rectangles

The corresponding boundary is the whole set of line segments drawn in Figure 2: 
 
Figure 2. The boundary of the set of rectangles

The vertices of all rectangles have integer coordinates. All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area. The numeric value of the result fits in a 32-bit signed representation.

PROGRAM NAME: picture

INPUT FORMAT

Line 1:N, the number of rectangles pasted on the wall.
Lines 2..N+1In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

SAMPLE INPUT (file picture.in)

7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16

OUTPUT FORMAT

A single line with a non-negative integer which corresponds to the perimeter for the input rectangles.

SAMPLE OUTPUT (file picture.out)

228



二 分析

分别考虑平行于X轴和Y轴的边。以X轴为例,一个矩形下方的边称之为下边,上方的边称之为上边。首先按照纵坐标从小到大排序。定义数组Level,如果来了一条纵坐标为a,左右横坐标分别为s,t的下边(a,,s,t),我们把Level[s],Level[s+1],...,Level[t-1]都加1,如果来了一条上边(a,s,t),则把Level[s],Level[s+1],...,Level[t-1]都减1。这样Level[i]就代表了目前[i, i+1]这一区间下方出现了多少个还没有上边匹配的下边。只有新增一条下边(a,s,t)使得Level[i]从0变为1,或新增一条上边(a,s,t)使得Leve[i]从1变成0才表明纵坐标为a的这条线段[i, i+1]出现在了最终区间的边缘,即对最终周长有贡献。值得注意的是,对于两个矩形边界重合的情况,如果是一条上边和一条下边重合,那么排序时应该把下边排在前面。


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: picture
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.011 secs, 5276 KB]
   Test 2: TEST OK [0.011 secs, 5276 KB]
   Test 3: TEST OK [0.000 secs, 5276 KB]
   Test 4: TEST OK [0.000 secs, 5276 KB]
   Test 5: TEST OK [0.000 secs, 5276 KB]
   Test 6: TEST OK [0.000 secs, 5276 KB]
   Test 7: TEST OK [0.011 secs, 5276 KB]
   Test 8: TEST OK [0.000 secs, 5276 KB]
   Test 9: TEST OK [0.011 secs, 5276 KB]
   Test 10: TEST OK [0.000 secs, 5276 KB]
   Test 11: TEST OK [0.097 secs, 5276 KB]

All tests OK.

YOUR PROGRAM ('picture') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.


AC代码:
/*
ID:maxkibb3
LANG:C++
PROB:picture
*/

#include<cstdio>
#include<algorithm>
using namespace std;

const int MAX = 1e5 + 5;
const int MAXR = 5005;

int N, Ans;
int Level[MAX * 2];

struct Seg {
    bool f;
    int a, s, t;
    Seg() {}
    Seg(bool _f, int _a, int _s, int _t)
        : f(_f), a(_a), s(_s), t(_t) {}
    bool operator < (const Seg &_o) const {
        if(a == _o.a) return f < _o.f;
        return a < _o.a;
    }
}Sx[MAXR * 2], Sy[MAXR * 2];

void init() {
    freopen("picture.in", "r", stdin);
    freopen("picture.out", "w", stdout);
    scanf("%d", &N);
    int a, b, c, d;
    for(int i = 0; i < N; i++) {
        scanf("%d%d%d%d", &a, &b, &c, &d);
        a += MAX, b += MAX, c += MAX, d += MAX;
        Seg sx1(false, b, a, c),
            sx2(true, d, a, c),
            sy1(false, a, b, d),
            sy2(true, c, b, d);
        Sx[i * 2] = sx1, Sx[i * 2 + 1] = sx2;
        Sy[i * 2] = sy1, Sy[i * 2 + 1] = sy2;
    }
    sort(Sx, Sx + 2 * N);
    sort(Sy, Sy + 2 * N);
}

void solve() {
    for(int i = 0; i < 2 * N; i++) {
        if(!Sx[i].f) {
            for(int j = Sx[i].s; j < Sx[i].t; j++) {
                Level[j]++;
                if(Level[j] == 1) Ans++;
            }
        }
        else {
            for(int j = Sx[i].s; j < Sx[i].t; j++) {
                Level[j]--;
                if(Level[j] == 0) Ans++;
            }
        }
    }
    for(int i = 0; i < MAX * 2; i++) Level[i] = 0;
    for(int i = 0; i < 2 * N; i++) {
        if(!Sy[i].f) {
            for(int j = Sy[i].s; j < Sy[i].t; j++) {
                Level[j]++;
                if(Level[j] == 1) Ans++;
            }
        }
        else {
            for(int j = Sy[i].s; j < Sy[i].t; j++) {
                Level[j]--;
                if(Level[j] == 0) Ans++;
            }
        }
    }
    printf("%d\n", Ans);
}

int main() {
    init();
    solve();
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值