zoukankan      html  css  js  c++  java
  • USACO Section 3.1 Shaping Regions (rect1)

    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
    
    思路:漂浮法吧,离散+线段树还不会= =,NOCOW上的标程也看不懂诶,还是自己写比较好
    
    Executing...
       Test 1: TEST OK [0.000 secs, 3252 KB]
       Test 2: TEST OK [0.000 secs, 3252 KB]
       Test 3: TEST OK [0.000 secs, 3252 KB]
       Test 4: TEST OK [0.000 secs, 3252 KB]
       Test 5: TEST OK [0.000 secs, 3252 KB]
       Test 6: TEST OK [0.000 secs, 3252 KB]
       Test 7: TEST OK [0.000 secs, 3252 KB]
       Test 8: TEST OK [0.000 secs, 3252 KB]
       Test 9: TEST OK [0.000 secs, 3252 KB]
       Test 10: TEST OK [0.000 secs, 3252 KB]
       Test 11: TEST OK [0.000 secs, 3252 KB]
    
    All tests OK.
      1 /*
      2 ID:wuhuaju2
      3 PROG:rect1
      4 LANG:C++
      5 */
      6 #include <cstdio>
      7 #include <iostream>
      8 #include <cstdlib>
      9 #include <algorithm>
     10 #include <cstring>
     11 #include <string>
     12 using namespace std;
     13 
     14 const int maxn=1010;
     15 const int maxcolor=2510;
     16 int color[maxn],ans[maxcolor];
     17 int i,kuan,chang,n,col;
     18 
     19 struct qq
     20 {
     21     int x1,x2,y1,y2;
     22 } a[maxn];
     23 
     24 void close()
     25 {
     26     fclose(stdin);
     27     fclose(stdout);
     28     exit(0);
     29 }
     30 
     31 int min(int a,int b)
     32 {
     33     if (a<b)
     34         return a;
     35     return b;
     36 }
     37 int max(int a,int b)
     38 {
     39     if (a>b)
     40         return a;
     41     return b;
     42 }
     43 
     44 void dfs(int x1,int y1,int x2,int y2,int k)
     45 {
     46     if (k==n+1)
     47     {
     48         ans[col]+=abs((x2-x1))*abs((y2-y1));
     49         return;
     50     }
     51     /*
     52     if ( (max(x1,x2)>=min(a[k].x1,a[k].x2)
     53         && max(a[k].x1,a[k].x2)>=min(x1,x2)
     54         && max(y1,y2)>=min(a[k].y1,a[k].y2)
     55         && max(a[k].y1,a[k].y2)>=min(y1,y2)
     56         )==false) dfs(x1,y1,x2,y2,k+1);
     57         */
     58     while (y1>a[k].y2 || y2<a[k].y1 || x2<a[k].x1 || x1>a[k].x2)
     59     {
     60         k++;
     61         if (k==n+1)
     62         {
     63             ans[col]+=(x2-x1)*(y2-y1);
     64             return;
     65         }
     66     } //如果两个矩形不相交,则直接到下一个矩形,剪枝非常有力
     67         if (x1<a[k].x1)
     68            dfs(x1,y1,a[k].x1,min(y2,a[k].y2),k+1);
     69         if (y1<a[k].y1)
     70            dfs(max(x1,a[k].x1),y1,x2,a[k].y1,k+1);
     71         if (y2>a[k].y2)
     72            dfs(x1,a[k].y2,min(x2,a[k].x2),y2,k+1);
     73         if (x2>a[k].x2)
     74            dfs(a[k].x2,max(y1,a[k].y1),x2,y2,k+1);
     75 }
     76 
     77 void init ()
     78 {
     79 freopen("rect1.in","r",stdin);
     80 freopen("rect1.out","w",stdout);
     81 memset(ans,0,sizeof(ans));
     82     scanf("%d %d %d",&kuan,&chang,&n);
     83      for (int i=1;i<=n;i++)
     84          scanf("%d %d %d %d %d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2,&color[i]);
     85      // ji suan zui shang mian de na ge ju xing de mian ji
     86      ans[color[n]]=(a[n].x2-a[n].x1)*(a[n].y2-a[n].y1);
     87      for (int i=n-1;i>=1;i--)
     88      {
     89     //     printf("i:%d \n",i);
     90          col=color[i];
     91          dfs(a[i].x1,a[i].y1,a[i].x2,a[i].y2,i+1);
     92      }
     93      int all=0;
     94      for (int i=2;i<=maxcolor;i++)
     95      {
     96          all+=ans[i];
     97      }
     98          ans[1]=kuan*chang-all;
     99          /*
    100      for (int i=1;i<=maxcolor;i++)
    101          if (ans[i]!=0)
    102              printf("color%d: ans:%d\n",i,ans[i]);
    103              */
    104          for (int i=1;i<=maxcolor;i++)
    105              if (ans[i]!=0)
    106                  printf("%d %d\n",i,ans[i]);
    107 }
    108 
    109 int main ()
    110 {
    111     init();
    112     close();
    113     return 0;
    114 }
    
    
    
     


  • 相关阅读:
    postgresql 高可用 etcd + patroni 之四 failover
    mysql 高可用架构 mha 之三 master_ip_online_change
    postgresql 一种比较个性的 sql 写法
    mysql 高可用架构 mha 之二 master_ip_failover
    mysql 高可用架构 mha 之一 安装
    vacuum 不释放文件系统空间
    mysql slave 复制冲突的解决
    mysql 8.0 登录报错
    oceanbase的一些网址信息
    cockroachdb的一些网址信息
  • 原文地址:https://www.cnblogs.com/cssystem/p/2910070.html
Copyright © 2011-2022 走看看