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
做这道题之前可以做这道题的简化题:https://mp.csdn.net/postedit/82829503
题目大意:一个矩形被分成若干个区域,给你一个坐标,判断在那个区域内,统计区域内玩具数为 t 的区域数,最后按 t 的升序输出
解题思路:
把每一个隔板看作一个向量,方向为从底边指向顶边,用a表示,另一个向量为从隔板底边定点指向所给坐标,
用b表示,则向量a叉乘向量b,如果结果为正,则该坐标在隔板左边位置,如果为负,则该坐标在隔板右边位置
注意:这种判断坐标在隔板左侧还是右侧仅使用于当前向量的取值与方向以及叉乘顺序。向量选取不同判断不同,当选用
其他向量时,可以先验证一下结果为正或者为负时,坐标在隔板的左边还是右边,因为具有通用性嘛,因为输入的隔板坐标不是从左到右的,所以我们需要排下序,按区域中玩具的数量升序输出,所以可以创建一个map容器保存结果
AC代码:
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=1e3+10;
struct node{
int x1,x2;
int num;
}a[maxn];
map<int,int> ans;//保存结果
bool cmp(node a,node b)
{
return a.x1<b.x1;//将隔板按从左到右的顺序排序,方便后面的统计
}
int main()
{
int n,m,X1,Y1,X2,Y2,i,j;
while(cin>>n,n)
{
ans.clear();
cin>>m>>X1>>Y1>>X2>>Y2;
a[0].x1=a[0].x2=X1;//矩形最右边的坐标
a[0].num=0;
a[n+1].x1=a[n+1].x2=X2;//矩形最左边的坐标
a[n+1].num=0;
for(i=1;i<=n;i++)
{
cin>>a[i].x1>>a[i].x2;
a[i].num=0;
}
sort(a+1,a+n+1,cmp);//隔板从左到右排序,隔板从a[1]开始保存
int U,L;
for(i=0;i<m;i++)
{
cin>>U>>L;
for(j=1;j<=n;j++)
{
int term=(a[j].x1-a[j].x2)*(L-Y2)-(Y1-Y2)*(U-a[j].x2);//叉乘
if(term>0)//结果为正,当前玩具在当前隔板左边区域
{
break;
}
}
a[j-1].num++;//当前隔板前一个区域内玩具数加一
}
for(i=0;i<=n;i++)
{
if(a[i].num)
{
ans[a[i].num]++;//统计玩具数为t的区域数
}
}
map<int,int>::iterator it;
cout<<"Box"<<endl;
for(it=ans.begin();it!=ans.end();it++)//输出
{
cout<<it->first<<": "<<it->second<<endl;
}
}
return 0;
}