您的位置:首页 > 大数据 > 人工智能

usaco training 5.5.1 Picture 题解

2014-03-15 14:06 489 查看
【原题】

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


【译题】


描述

N(N<5000) 张矩形的海报,照片和其他同样形状的图片贴在墙上。它们的边都是垂直的或水平的。每个矩形可以部分或者全部覆盖其他矩形。所有的矩形组成的集合的轮廓称为周长。写一个程序计算周长。

图 1 是一个有 7 个矩形的例子:



图 1.一个 7 个矩形的集合

对应的轮廓为图 2 所示的所有线段的集合:



图 2. 矩形集合的轮廓

所有矩形的顶点坐标均为整数。所有的坐标都在 [-10000,10000] 的范围内,并且任何一个矩形面积都为整数。结果的值可能需要 32 位有符号整数表示。


格式

PROGRAM NAME: picture

INPUT FORMAT:

(file picture.in)

第1行: N,张贴在墙上的矩形的数目。 第 2..N+1行 接下来的N行中,每行都有两个点的坐标,分别是矩形的左下角坐标和右上角坐标。每一个坐标由 X 坐标和 Y 坐标组成。

OUTPUT FORMAT:

(file picture.out) 只有一行,为一个非负整数,表示输入数据中所有矩形集合的轮廓长度。


SAMPLE INPUT

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


SAMPLE OUTPUT

228


【序言】以前在月赛中也看到一道题,好像是城市中的地平线。这两题都是矩阵的运算,而且范围很大。不过这道题很特别,是求周长的!正解应该是线段树维护区间覆盖,但是写起来会很麻烦。网上说直接暴力就能A!试了试。

【分析】我们把矩形的坐标先离散。以处理横条为例。------->>>>>>

①我们在离散每一条横条时记录它们的纵坐标,同时记录这一横条是矩形的下边(始边)还是上边(终边)。然后把它们按纵坐标从小到大排好。

②从小到大循环每一个横条。如果是始边,那么把它代表的区间每个格子都加1,否则减1(当然这样效率有点低,最好要用线段树优化)。如果某个格子是始边且原先是0,那么我们就把ans++。如果某个格子是终边且原先是1,那么我们也把ans++。(具体原理纸上推推就行了)

【代码】

/*
PROG:picture
ID:juan1973
LANG:C++
*/
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=5005;
const int size=20005;
const int zero=10000;
struct node{int x,y,f;bool ok;}a[2][maxn*2];
int level[size];
int n,i,x1,x2,y1,y2,ans;
bool cmp(node c,node d){return c.f<d.f||c.f==d.f&&c.ok;}
void find(int o)
{
sort(a[o]+1,a[o]+n+1,cmp);
memset(level,0,sizeof(level));
int j;
for (int i=1;i<=n;i++)
{
if (a[o][i].ok)
{
for (j=a[o][i].x;j<a[o][i].y;j++)
{
level[j]++;
if (level[j]==1) ans++;
}
}
else
{
for (j=a[o][i].x;j<a[o][i].y;j++)
{
level[j]--;
if (level[j]==0) ans++;
}
}
}
}
int main()
{
freopen("picture.in","r",stdin);
freopen("picture.out","w",stdout);
scanf("%d",&n);
for (i=1;i<=n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1+=zero;x2+=zero;y1+=zero;y2+=zero;
a[0][i*2-1].x=x1;a[0][i*2-1].y=x2;a[0][i*2-1].ok=true;a[0][i*2-1].f=y1;
a[0][i*2].x=x1;a[0][i*2].y=x2;a[0][i*2].ok=false;a[0][i*2].f=y2;
a[1][i*2-1].x=y1;a[1][i*2-1].y=y2;a[1][i*2-1].ok=true;a[1][i*2-1].f=x1;
a[1][i*2].x=y1;a[1][i*2].y=y2;a[1][i*2].ok=false;a[1][i*2].f=x2;
}
n=n*2;
find(0);
find(1);
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  usaco training 题解