zoukankan      html  css  js  c++  java
  • POJ2398 Toy Storage

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5523   Accepted: 3288

    Description

    Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for Reza to find his favorite toys anymore. 
    Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top: 

    We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.

    Input

    The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1, y1) and (x2, y2), respectively. The following n lines each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard. 

    A line consisting of a single 0 terminates the input.

    Output

    For each box, first provide a header stating "Box" on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.

    Sample Input

    4 10 0 10 100 0
    20 20
    80 80
    60 60
    40 40
    5 10
    15 10
    95 10
    25 10
    65 10
    75 10
    35 10
    45 10
    55 10
    85 10
    5 6 0 10 60 0
    4 3
    15 30
    3 1
    6 8
    10 10
    2 1
    2 8
    1 5
    5 5
    40 10
    7 9
    0
    

    Sample Output

    Box
    2: 5
    Box
    1: 4
    2: 1
    

    Source

    数学 几何

    比较有趣

    对于每个点,二分选择线段来夹逼它,得出它所在的区间,统计答案。

     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<vector>
     8 using namespace std;
     9 const double eps=1e-8;
    10 const int mxn=1010;
    11 int read(){
    12     int x=0,f=1;char ch=getchar();
    13     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    14     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    15     return x*f;
    16 }
    17 struct Point{
    18     double x,y;
    19     Point operator + (Point rhs){return (Point){x+rhs.x,y+rhs.y};}
    20     Point operator - (Point rhs){return (Point){x-rhs.x,y-rhs.y};}
    21     Point operator * (double len){return (Point){x*len,y*len};}
    22     double operator *(Point rhs){return x*rhs.x+y*rhs.y;}
    23 }a[mxn],b[mxn],p;
    24 struct Line{
    25     double u,l;
    26     bool operator < (Line rhs)const {return u<rhs.u;}
    27 }l[mxn];
    28 double Cross(Point a,Point b){
    29     return a.x*b.y-a.y*b.x;
    30 }
    31 int n,m;
    32 int w[mxn],cnt[mxn];
    33 int main(){
    34     int i,j;
    35     while(1){
    36         n=read();
    37         if(!n)break;
    38         memset(w,0,sizeof w);
    39         memset(cnt,0,sizeof cnt);
    40         m=read();
    41         a[0].x=read();a[0].y=read();//左上 
    42         b[n+1].x=read();b[n+1].y=read();//右下 
    43         
    44 //        printf("%.f %.f %.f %.f
    ",a[0].x,a[0].y,b[n+1].x,b[n+1].y);
    45         
    46         a[n+1].x=b[n+1].x;a[n+1].y=a[0].y;
    47         b[0].x=a[0].x;b[0].y=b[n+1].y;
    48         
    49 //        printf("%.f %.f %.f %.f
    ",a[n+1].x,a[n+1].y,b[0].x,b[0].y);
    50         
    51         for(i=1;i<=n;i++){l[i].u=read();l[i].l=read();}
    52         sort(l+1,l+n+1);
    53         for(i=1;i<=n;i++){
    54             a[i].x=l[i].u;a[i].y=a[0].y;
    55             b[i].x=l[i].l;b[i].y=b[0].y;
    56         }
    57         for(i=1;i<=m;i++){
    58             p.x=read();p.y=read();
    59 //            printf("%.2f %.2f %.2f %.2f
    ",a[0].x,a[n+1].y,a[n+1].x,b[n+1].y);
    60 //            printf("%.f %.f
    ",p.x,p.y);
    61             if(p.x<a[0].x || p.x>a[n+1].x || p.y>a[0].y || p.y<b[n+1].y)continue;
    62             int l=0,r=n+1;
    63             while(l!=r-1){
    64                 int mid=(l+r)>>1;
    65                 if(Cross(a[mid]-b[mid],p-b[mid])>0)r=mid;
    66                 else l=mid;
    67                 
    68             }
    69 //            printf("l:%d
    ",l);
    70             ++cnt[l];
    71         }
    72         for(i=0;i<=n;i++){
    73             if(cnt[i])w[cnt[i]]++;
    74         }
    75         printf("Box
    ");
    76         for(i=1;i<=m;i++){
    77             if(w[i]){
    78                 printf("%d: %d
    ",i,w[i]);
    79             }
    80         }
    81     }
    82     return 0;
    83 }
    本文为博主原创文章,转载请注明出处。
  • 相关阅读:
    块级元素与行级元素(内联元素)
    css中属性值继承小解
    form表单
    html,xhtml和xml
    html中的标签分类
    如何把HTML标记分类
    实现对HashMap的value排序
    装饰者模式
    实现一个简单的二叉树容器,并且实现中序、先序、后续遍历
    Java中java.util.concurrent包下的4中线程池代码示例
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6486236.html
Copyright © 2011-2022 走看看