usaco 5.3 Window Area(模拟+矩形切割)

来源:互联网 发布:广州用友java面试题 编辑:程序博客网 时间:2024/06/06 13:07
Window Area
IV Balkan Olympiad

You've just be assigned the project of implemented a windowing interface. This windowing interface is fairly simple, and fortunately, you don't have to display the actual windows. There are 5 basic operations:

  1. Create a window
  2. Bring a window to the top
  3. Put a window to the bottom
  4. Destroy a window
  5. Output what percentage of a window is visible (i.e., isn't covered by windows above it).

In the input, the operations appear in the following format:

  • Create window: w(I,x,y,X,Y)
  • Bring window to top: t(I)
  • Put window on bottom: b(I)
  • Destroy window: d(I)
  • Output percentage visible: s(I)
The I is a unique identifier for each window, which is one character. The character can be any of 'a'..'z', 'A'..'Z', and '0'..'9'. No extra spaces will appear in the input.

(x,y) and (X,Y) are opposite corners of the window. When a window is created, it is put `on top'. You can't create a window with an identifier that is already in use, but you can destroy a window and then create a new one with the identifier of the destroyed window. Coordinates will be positive integers, and all windows will be of non-zero area (x != X and y != Y). The x and y coordinates are between 1 and 32767 inclusive.

PROGRAM NAME: window

INPUT FORMAT

The input file consists of a sequence of commands to your interpreter. They will be listed one per line. Terminate the program when no more input is available

SAMPLE INPUT (file window.in)

w(a,10,132,20,12)w(b,8,76,124,15)s(a)

OUTPUT FORMAT

Output lines only for the s() commands. Of course, there might be several s() commands (but no more than 500) so the output should be a sequence of percentages, one per line, stating the percentage of the windows that are visible. The percentages should be rounded to 3 decimal places.

SAMPLE OUTPUT (file window.out)

49.167

题目:让你模拟一些窗口的创建移动等操作,输出每次给定窗口的可见度。。。

分析:由于数据相当的小,直接模拟就行,面积的计算用矩形切割就行,现在发现矩形切割对于数据小的题还是很好用的。。。。

代码:

/*ID: 15114582PROG: windowLANG: C++*/#include<cstdio>#include<cstring>#include<iostream>using namespace std;struct win{    int l,r,t,b;}w[999];int live[256],p[999];int bot,top,x1,y1,x2,y2;double see;char op,id,s[256];void cut(int i,int l,int r,int t,int b){    while(i<top&&(r<=w[i].l||l>=w[i].r||b<=w[i].t||t>=w[i].b))++i;    if(i>=top)see+=(r-l)*(b-t);    else    {        if(l<w[i].l)cut(i+1,l,w[i].l,t,b),l=w[i].l;        if(r>w[i].r)cut(i+1,w[i].r,r,t,b),r=w[i].r;        if(t<w[i].t)cut(i+1,l,r,t,w[i].t);        if(b>w[i].b)cut(i+1,l,r,w[i].b,b);    }}int main(){    freopen("window.in","r",stdin);    freopen("window.out","w",stdout);    memset(live,0,sizeof(live));    memset(p,0,sizeof(p));    bot=top=256;    while(~scanf("%s",s))    {        if(s[0]=='w')        {            sscanf(s,"%c(%c,%d,%d,%d,%d)",&op,&id,&x1,&y1,&x2,&y2);            if(x1>x2)swap(x1,x2);            if(y1>y2)swap(y1,y2);            w[top].l=y1,w[top].r=y2;            w[top].t=x1,w[top].b=x2;            live[id]=top;            p[top++]=id;        }        if(s[0]=='t')        {            for(int i=live[s[2]];i<top-1;++i)            {                swap(live[p[i]],live[p[i+1]]);                swap(p[i],p[i+1]);                swap(w[i],w[i+1]);            }        }        if(s[0]=='b')        {            for(int i=live[s[2]];i>bot;--i)            {                swap(live[p[i]],live[p[i-1]]);                swap(p[i],p[i-1]);                swap(w[i],w[i-1]);            }        }        if(s[0]=='d')        {            for(int i=live[s[2]];i<top-1;++i)            {                swap(live[p[i]],live[p[i+1]]);                swap(p[i],p[i+1]);                swap(w[i],w[i+1]);            }            --top;        }        if(s[0]=='s')        {            int i=live[s[2]];            double sum=(w[i].r-w[i].l)*(w[i].b-w[i].t);            see=0;            cut(i+1,w[i].l,w[i].r,w[i].t,w[i].b);            printf("%.3lf\n",see/sum*100);        }    }    return 0;}


原创粉丝点击