poj 2318 TOYS (计算几何)

TOYS
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12453   Accepted: 6013

Description

Calculate the number of toys that land in each bin of a partitioned toy box. 
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John 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 John to find his favorite toys. 

John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box. 
 
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.

Input

The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). 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 contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.

Output

The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.

Sample Input

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

Sample Output

  1 /*************************************************************************
  2     > File Name: code/poj/2318.cpp
  3     > Author: 111qqz
  4     > Email: rkz2013@126.com 
  5     > Created Time: 2015年11月08日 星期日 10时09分41秒
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 using namespace std;
 21 #define REP(i, n) for (int i=0;i<int(n);++i)  
 22 typedef long long LL;
 23 typedef unsigned long long ULL;
 24 const int N=5E3+5;
 25 struct node
 26 {
 27     int x,y;
 28 };
 29 struct node rec,rec2;
 30 struct node par[N],par2[N];
 31 struct node toy[N];
 32 int ans[N],cnt[N];
 33 int n,m;
 34 bool judge(node p1,node p2,node p3) //判断点是否在直线的[右侧!!]
 35 {
 36     int s = (p1.x-p3.x)*(p2.y-p3.y)-(p1.y-p3.y)*(p2.x-p3.x);
 37     if (s>0) return false;
 38     if (s<0) return true;
 39 }
 40 bool cmp(node a,node b)
 41 {
 42     if (a.x<b.x) return true;
 43     if (a.x==b.x&&a.y<b.y) return true;
 44     return false;
 45 }
 46 int main()
 47 {
 48 //    freopen("in.txt","r",stdin);
 49     while (scanf("%d",&n)!=EOF&&n)
 50     {
 51       memset(ans,0,sizeof(ans));
 52       memset(par,0,sizeof(par));
 53       memset(par2,0,sizeof(par2));
 54       memset(toy,0,sizeof(toy));
 55       cin>>m>>rec.x>>rec.y>>rec2.x>>rec2.y;
 56       for ( int i = 1 ;  i <= n ; i++)
 57       {
 58         cin>>par[i].x>>par2[i].x;
 59         par[i].y=rec.y;
 60         par2[i].y=rec2.y;
 61       }
 62       for ( int i = 1 ; i <= n-1 ; i++)
 63       {
 64         for ( int j = i+1 ; j <= n ; j++)
 65         {
 66             if (par[i].x>par[j].x)
 67             {
 68               swap(par[i].x,par[j].x);
 69             //  swap(par[i].y,par[j].y);
 70               swap(par2[i].x,par2[j].x);
 71             //   swap(par2[i].y,par2[j].y);
 72             }
 73         }
 74       }
 75 //      for ( int i = 1 ;  i <= n ; i++)
 76 //        cout<<par[i].x<<endl;
 77       for ( int i = 1 ;  i <= m ; i++ )
 78        {
 79         cin>>toy[i].x>>toy[i].y;
 80        }
 81       int p;
 82       sort(toy+1,toy+m+1,cmp);  //如果第i个娃娃在第k个分划中,那么排序后第i+1个娃娃至少在第k个分划中....(某大神说过,顺手就能写的优化顺手    
 83 //      for ( int i = 1 ; i <= m ; i++)  cout<<"x[i]:"<<toy[i].x<<" y[i]:"<<toy[i].y<<endl;
 84       for ( int i = 1 ; i  <= m ;  i++) 
 85       {
 86         p = n + 1;  //如果在所有board的右侧,那么一定是在最后一个分划中(n个板子形成n+1个分划)
 87         bool ok=false;
 88         for ( int j = 1 ; j  <= n ; j++)
 89         {
 90             ok=judge(par2[j],par[j],toy[i]);
 91             if (!ok)
 92             {
 93         //      cout<<"i:"<<i<<" j:"<<j<<" "<<par2[j].x<<" "<<par2[j].y<<" "<<par[j].x<<" "<<par[j].y<<endl;
 94               p = j;
 95               break;
 96             //    cout<< "hhhhhh"<<"I:"<<i<<" j:"<<j<<endl;
 97             }
 98         }
 99         ans[p]++;
100       }
101 //      cout<<"Box"<<endl;
102       memset(cnt,0,sizeof(cnt));
103       for ( int i = 1 ; i <= n+1 ; i++)
104       {
105 //        if (ans[i]==0) continue;
106 //        cnt[ans[i]]++;
107         printf("%d: %d
",i-1,ans[i]);
108       } 
109       puts("");
110 //      for ( int i = 1 ; i <= m ;  i++)
111 //      {
112 //        if (cnt[i]==0) continue;
113 //          printf("%d: %d
",i,cnt[i]);
114 //      }
115     }
116     return 0;
117 }
View Code
0: 2
1: 1
2: 1
3: 1
4: 0
5: 1

0: 2
1: 2
2: 2
3: 2
4: 2

Hint

As the example illustrates, toys that fall on the boundary of the box are "in" the box.
 
直接上代码
 
原文地址:https://www.cnblogs.com/111qqz/p/4947424.html