• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            USACO Section 3.1 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 and 8,0 and a 4 and 8,1 but NOT a 4 and 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
            Shaping Regions: Hint
            An array of all 'points' is too big; 16MB maximum. Keep track of the rectangles' coordinates; split the rectangle when an overlap occurs, e.g.: 
            +--------+      +-+--+--+
            |        |      | |2 |  |
            |        |      + +--+  |
            |  +-+   |  --> | |  |  |
            |  +-+   |      |1|  |3 |
            |        |      | +--+  |
            |        |      | | 4|  |
            +--------+      +-+--+--+
             
            Official analysis

            A straightforward approach to this problem would be to make an array which represents the table, and then draw all the rectangles on it. In the end, the program can just count the colors from the array and output them. Unfortunately, the maximum dimensions of this problem are 10,000 x 10,000, which means the program uses 100 million integers. That's too much, so we need another approach.

            An approach that does work for such large cases (and it actually is a lot faster too) is to keep track of the rectangles, and delete portions of them when they are covered by other rectangles. 
            Consider this input set: 
            0 0 10 10 5
            5 0 15 10 10
            

            The program first reads in the first rectangle and puts it in a list. When it reads a new rectangle it checks all items in the list if they overlap with the new rectangle. This is the case, and then it deletes the old rectangle from the list and adds all parts which aren't covered to the list. (So in this case, the program would delete the first rectangle, add 0 0 5 10 5 to the list and then add the second rectangle to the list). `` If you're unlucky, a new rectangle can create lots of new rectangles (when the new rectangle entirely fits into another one, the program creates four new rectangles which represent the leftover border:

            +--------+      +-+--+--+
            |        |      | |2 |  |
            |        |      + +--+  |
            |  +-+   |  --> | |  |  |
            |  +-+   |      |1|  |3 |
            |        |      | +--+  |
            |        |      | | 4|  |
            +--------+      +-+--+--+

            This is not a problem however, because there can be only 2500 rectangles and there is plenty of memory, so rectangles have to be cut very much to run out of memory. Note that with this approach, the only thing that matters is how many rectangles there are and how often they overlap. The maximum dimensions can be as large as you want, it doesn't matter for the running time.

            There is another solution to this problem, which runs in O(n*n*log n) time, but is quite tricky. First, we add one big white rectangle at the bottom - the paper. Then we make two arrays: one containing all vertical edges of the rectangles, and the other the horizontal ones. For each edge we have its coordinates and remember, whether it's the left or right edge (top or bottom). We sort these edges from left to right and from top to bottom. Then we go from left to right (sweep), jumping to every x-coordinate of vertical edges. At each step we update the set of rectangles seen. We also want to update the amount of each color seen so far. So for each x we go from top to bottom, for each y updating the set of rectagles at a point (in the structure described below) and choosing the top one, so that we can update the amounts of colors seen. The structure to hold the set of rectangles at a point should allow adding a rectangle (number from 1..1000), deleting a rectangle, and finding the top one. We can implement these operations in O(log n) time if we use a heap. To make adding and deleting run in O(log n) we must also have for each rectangle its position in the heap. So the total time spent at each point is O(log n). Thus the algorithm works in O(n*n*log n) time.

            Official Code

             

            #include <stdio.h>
            #include 
            <stdlib.h>
            #include 
            <string.h>

            FILE 
            *fp,*fo;

            struct rect
            {
                
            int c;
                
            int x1,y1,x2,y2;
            }
            ;

            int c[2501];
            rect r[
            10001];

            int intersect(rect a,const rect &b,rect out[4])
            {
                
            /* do they at all intersect? */
                
            if(b.x2<a.x1||b.x1>=a.x2)
                    
            return 0;
                
            if(b.y2<a.y1||b.y1>=a.y2)
                    
            return 0;
                
            /* they do */

                rect t;

                
            if(b.x1<=a.x1&&b.x2>=a.x2&&b.y1<=a.y1&&b.y2>=a.y2)
                        
            return -1;

                
            /* cutting `a' down to match b */
                
            int nout=0;
                
            if(b.x1>=a.x1) {
                    t
            =a,t.x2=b.x1;
                    
            if(t.x1!=t.x2)
                        
            out[nout++]=t;
                    a.x1
            =b.x1;
                }

                
            if(b.x2<a.x2) {
                    t
            =a,t.x1=b.x2;
                    
            if(t.x1!=t.x2)
                        
            out[nout++]=t;
                    a.x2
            =b.x2;
                }

                
            if(b.y1>=a.y1) {
                    t
            =a,t.y2=b.y1;
                    
            if(t.y1!=t.y2)
                        
            out[nout++]=t;
                    a.y1
            =b.y1;
                }

                
            if(b.y2<a.y2) {
                    t
            =a,t.y1=b.y2;
                    
            if(t.y1!=t.y2)
                        
            out[nout++]=t;
                    a.y2
            =b.y2;
                }

                
            return nout;
            }


            int main(void{
                fp
            =fopen("rect1.in","rt");
                fo
            =fopen("rect1.out","wt");

                
            int a,b,n;
                fscanf(fp,
            "%d %d %d",&a,&b,&n);

                r[
            0].c=1;
                r[
            0].x1=r[0].y1=0;
                r[
            0].x2=a;
                r[
            0].y2=b;

                rect t[
            4];

                
            int i,j,rr=1;
                
            for(i=0;i<n;i++{
                    
            int tmp;
                    fscanf(fp,
            "%d %d %d %d %d",&r[rr].x1,&r[rr].y1,&r[rr].x2,&r[rr].y2,&r[rr].c);

                    
            if(r[rr].x1>r[rr].x2) {
                        tmp
            =r[rr].x1;
                        r[rr].x1
            =r[rr].x2;
                        r[rr].x2
            =tmp;
                    }

                    
            if(r[rr].y1>r[rr].y2) {
                        tmp
            =r[rr].y1;
                        r[rr].y1
            =r[rr].y2;
                        r[rr].y2
            =tmp;
                    }


                    
            int nr=rr;
                    rect curr
            =r[rr++];
                    
            for(j=0;j<nr;j++{
                        
            int n=intersect(r[j],curr,t);
                        
            if(!n)
                            
            continue;
                        
            if(n==-1{
                            memmove(r
            +j,r+j+1,sizeof(rect)*(rr-j-1));
                            j
            --;
                            rr
            --;
                            nr
            --;
                            
            continue;
                        }

                        r[j]
            =t[--n];
                        
            for(;n-->0;)
                            r[rr
            ++]=t[n];
                    }

                }


                
            for(i=0;i<rr;i++)
                    c[r[i].c]
            +=(r[i].x2-r[i].x1)*(r[i].y2-r[i].y1);

                
            for(i=1;i<=2500;i++)
                    
            if(c[i])
                        fprintf(fo,
            "%d %d\n",i,c[i]);

                
            return 0;
            }


            My Code


            /*
            ID:braytay1
            PROG:rect1
            mble
            LANG:C++
            */

            #include 
            <iostream>
            #include 
            <fstream>
            using namespace std;

            struct rectangle{
                
            int lx;
                
            int ly;
                
            int rx;
                
            int ry;
                
            int color;
            }
            ;

            int A,B,N;
            int xs[2002],ys[2002];
            int area[2501];
            rectangle rec[
            1001];

            void swap(int *p1,int *p2){
                
            int tmp;
                tmp
            =*p1;
                
            *p1=*p2;
                
            *p2=tmp;
            }

            int partition(int a[],int p,int r){
                
            int x,i;
                x
            =a[r];
                i
            =p-1;
                
            for (int j=p;j<r;j++)
                
            {
                    
            if (a[j]<=x) {i++;swap(a+i,a+j);}
                }

                swap(a
            +i+1,a+r);
                
            return i+1;
            }

            void quicksort(int a[],int p,int r){
                
            if (p<r)
                
            {
                    
            int q;
                    q
            =partition(a,p,r);
                    quicksort(a,p,q
            -1);
                    quicksort(a,q
            +1,r);
                }

            }




            int color_num(rectangle &a){
                
            for (int i=N;i>=0;i--){
                    
            if ((a.lx>=rec[i].lx)&&(a.rx<=rec[i].rx)&&(a.ly>=rec[i].ly)&&(a.ry<=rec[i].ry))
                        
            return rec[i].color;
                }

                
            return 1;
            }

            int reform(int *p,int n){
                
            int tmp[2002],cur_num;
                tmp[
            0]=*p;
                cur_num
            =0;
                
            for (int i=1;i<=n;i++){
                    
            if (tmp[cur_num]!=*(p+i)) {
                        cur_num
            ++;
                        tmp[cur_num]
            =*(p+i);
                    }

                }

                
            for (int i=0;i<=n;i++*(p+i)=0;
                
            for (int i=0;i<=cur_num;i++*(p+i)=tmp[i];
                
            return cur_num;
            }

            int main(){
                ifstream fin(
            "rect1.in");
                ofstream fout(
            "rect1.out");
                memset(area,
            0,sizeof(area));
                fin
            >>A>>B>>N;
                rec[
            0].lx=0;rec[0].ly=0;rec[0].rx=A;rec[0].ry=B;rec[0].color=1;
                xs[
            0]=0;xs[1]=A;ys[0]=0;ys[1]=B;
                
            for (int i=1;i<=N;i++){
                    fin
            >>rec[i].lx>>rec[i].ly>>rec[i].rx>>rec[i].ry>>rec[i].color;
                    xs[i
            *2]=rec[i].lx;
                    xs[i
            *2+1]=rec[i].rx;
                    ys[i
            *2]=rec[i].ly;
                    ys[i
            *2+1]=rec[i].ry;
                }

                
            int xlong,ylong;
                quicksort(xs,
            0,2*N+1);
                quicksort(ys,
            0,2*N+1);
                xlong
            =reform(xs,2*N+1);
                ylong
            =reform(ys,2*N+1);
                quicksort(xs,
            0,xlong);
                quicksort(ys,
            0,ylong);
                
            int k=0;
                
            for (int i=0;i<ylong;i++){
                    
            for (int j=0;j<xlong;j++){
                        rectangle single;
                        
            if (xs[j]==xs[j+1]||ys[i]==ys[i+1]) continue;
                        single.lx
            =xs[j];single.ly=ys[i];single.rx=xs[j+1];single.ry=ys[i+1];
                        single.color
            =color_num(single);
                        area[single.color
            -1]+=(single.rx-single.lx)*(single.ry-single.ly);
                    }

                }

                
            for (int i=0;i<2500;i++)
                    
            if (area[i]) fout<<i+1<<" "<<area[i]<<endl;
                
            return 0;
            }

            posted on 2008-08-21 18:11 幻浪天空領主 閱讀(599) 評論(0)  編輯 收藏 引用 所屬分類: USACO

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案(2)

            文章分類(23)

            文章檔案(22)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久精品久久久久久清纯| 亚洲AV无一区二区三区久久| 色综合合久久天天综合绕视看| 精品免费久久久久国产一区| 色综合久久中文字幕综合网| 久久久一本精品99久久精品66| 日本三级久久网| 久久精品aⅴ无码中文字字幕不卡| WWW婷婷AV久久久影片| 亚洲午夜无码AV毛片久久| 久久久青草久久久青草| av色综合久久天堂av色综合在| 久久艹国产| 久久久91精品国产一区二区三区| 亚洲欧洲中文日韩久久AV乱码| 久久91精品国产91久久麻豆| 久久久无码精品亚洲日韩京东传媒 | 久久狠狠一本精品综合网| 精品国产青草久久久久福利| 欧美精品福利视频一区二区三区久久久精品 | 国产精品一久久香蕉国产线看观看| 久久精品亚洲乱码伦伦中文| 久久久久成人精品无码中文字幕 | 狠狠久久综合伊人不卡| 国产精品久久久久影院色| 亚洲国产欧洲综合997久久| 欧洲性大片xxxxx久久久| 亚洲欧美精品伊人久久| 国产精品久久影院| 久久久精品午夜免费不卡| 国产成人久久AV免费| 久久国产亚洲高清观看| 久久久久久国产精品免费无码 | 日韩美女18网站久久精品| 久久黄视频| 亚洲综合久久久| 久久婷婷人人澡人人爽人人爱| 久久综合九色综合网站| 亚洲AV无一区二区三区久久| 久久久久人妻一区二区三区vr | 久久精品国产只有精品66|