2398 Toy Storage
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
題目大意:一個矩形被分成若干個區域,給你一個座標,判斷在那個區域內,統計區域內玩具數為 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;
}