usaco6.2.3 Shaping Regions

一 原题

Shaping Regions

N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the sheet's borders. All rectangles fall within the borders of the sheet so that different figures of different colors will be seen.

The coordinate system has its origin (0,0) at the sheet's lower left corner with axes parallel to the sheet's borders.

PROGRAM NAME: rect1

INPUT FORMAT

The order of the input lines dictates the order of laying down the rectangles. The first input line is a rectangle "on the bottom".

Line 1:A, B, and N, space separated (1 <= A,B <= 10,000)
Lines 2-N+1:Five integers: llx, lly, urx, ury, color: the lower left coordinates and upper right coordinates of the rectangle whose color is `color' (1 <= color <= 2500) to be placed on the white sheet. The color 1 is the same color of white as the sheet upon which the rectangles are placed.

SAMPLE INPUT (file rect1.in)

20 20 3
2 2 18 18 2
0 8 19 19 3
8 0 10 19 4

INPUT EXPLANATION

Note that the rectangle delineated by 0,0 and 2,2 is two units wide and two high. Here's a schematic diagram of the input:
11111111111111111111
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
33333333443333333331
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11222222442222222211
11111111441111111111
11111111441111111111
The '4's at 8,0 to 10,19 are only two wide, not three (i.e., the grid contains a 4 at 8,0 and a 4 at 8,1 but NOT a 4 at 8,2 since this diagram can't capture what would be shown on graph paper).

OUTPUT FORMAT

The output file should contain a list of all the colors that can be seen along with the total area of each color that can be seen (even if the regions of color are disjoint), ordered by increasing color. Do not display colors with no area.

SAMPLE OUTPUT (file rect1.out)

1 91
2 84
3 187
4 38


二 分析

在一张白纸上给定n个矩形的摆放顺序,摆放时矩形的边和白纸的边平行。矩形之间会互相重叠,求最后每个矩形能见到的面积。
可以想象n个矩形在水底依次向水面漂浮,如果被先前的矩形挡住了,被挡住的部分就不再上浮。对应的实现主要就是up(idx, d, x1, y1, x2, y2)这个函数,代表第idx个矩形的一部分(矩形切割的结果还是矩形,用x1,y1,x2,y2表示左下和右上角的坐标)上浮到第d层,当d=n时,就代表这部分矩形已经上浮到水面,更新对应的结果就可以了。
这题输入里是可能有相同标号的矩形的,题目中底色为1,甚至还会有标号为1的矩形。第一次交就因为这点悲剧了。


三 代码

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

Compiling...
Compile: OK

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

All tests OK.

Your program ('rect1') produced all correct answers! This is your submission #2 for this problem. Congratulations!



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

#include<cstdio>
#include<algorithm>

const int MAX = 2505;

int n, ans[MAX];

struct Rect {
    int x1, y1, x2, y2, id;
}r[MAX];

void up(int idx, int d, int x1, int y1, int x2, int y2) {
    if(x2 <= x1 || y2 <= y1)
        return;
    if(d == n) {
        ans[r[idx].id] += (x2 - x1) * (y2 - y1);
        return;
    }
    
    up(idx, d + 1, x1, y1, std::min(r[d].x1, x2), y2);

    up(idx, d + 1, std::max(r[d].x2, x1), y1, x2, y2);

    up(idx, d + 1, std::max(x1, r[d].x1), y1, std::min(x2, r[d].x2), std::min(r[d].y1, y2));

    up(idx, d + 1, std::max(x1, r[d].x1), std::max(r[d].y2, y1), std::min(x2, r[d].x2), y2);
}

int main() {
    freopen("rect1.in", "r", stdin);
    freopen("rect1.out", "w", stdout);
    scanf("%d%d%d", &r[0].x2, &r[0].y2, &n);
    n++;
    r[0].id = 1;
    for(int i = 1; i < n; i++)
        scanf("%d%d%d%d%d", &r[i].x1, &r[i].y1,
            &r[i].x2, &r[i].y2, &r[i].id);

    for(int i = n - 1; i >= 0; i--) {
        up(i, i + 1, r[i].x1, r[i].y1, r[i].x2, r[i].y2);
    }

    for(int i = 1; i < MAX; i++) {
        if(ans[i] == 0)
            continue;
        printf("%d %d\n", i, ans[i]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值