欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Toy Storage(点和直线位置关系)利用叉积

程序员文章站 2022-03-30 08:54:28
...

题目:https://vjudge.net/contest/242905#problem/B

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: 

Toy Storage(点和直线位置关系)利用叉积


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个玩具的箱子有多少个。

用了直线,和以前的题用

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#define maxn 1005
using namespace std;
struct Point {
	double x, y;
	Point(double x=0, double y=0): x(x), y(y) {}					//构造函数
	Point operator + (Point p) {return Point(x+p.x, y+p.y);}				//两向量相加
	Point operator - (Point p) {return Point(x-p.x, y-p.y);}				//两向量相减
	Point operator * (double k) {return Point(k*x, k*y);}				//向量的数乘
	Point operator / (double k) {return Point(x/k, y/k);}				//向量的数乘(乘以一个分数)
	bool operator < (const Point &p) const {return x!=p.x ? (x<p.x):(y<p.y);}	//给点排序时使用(可以根据需要变化)
	double norm() {return x*x + y*y;}							//向量的范数
	double abs() {return sqrt(norm());}							//向量的大小
	double dot(Point p) {return x*p.x + y*p.y;}					//两向量内积
	double cross(Point p) {return x*p.y - y*p.x;}					//两向量外积
};

struct line
{
    Point s,e;
    line (){}
    line (Point _s,Point _e)
    {
        s=_s;e=_e;
    }
};
line Line[maxn];
int ccw(Point p0, Point p1, Point p2) {
	Point v1 = p1-p0;
	Point v2 = p2-p0;
	if(v1.cross(v2) > 0) return 1;			//逆时针方向
	if(v1.cross(v2) < 0) return -1;			//顺时针方向
	if(v1.dot(v2) < 0) return 2;			//同一直线上的反方向
	if(v1.norm() < v2.norm()) return -2;	//同一直线上的正方向
	return 0;								//在向量上
}
bool cmp(line a,line b)
{
    return a.s.x<b.s.x;
}
int main()
{
    int n,m;
    double x1,x2,y1,y2;
    while(scanf("%d%d%lf%lf%lf%lf",&n,&m,&x1,&y1,&x2,&y2)==6&&n)
    {
        double U,L,xi,yi;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&U,&L);
           Line[i]=line(Point(U,y1),Point(L,y2));
        }
        Line[n]=line(Point(x2,y1),Point(x2,y2));
        sort(Line,Line+n+1,cmp);
        int ans[maxn];
        memset(ans,0,sizeof(ans));
        Point pp;
        while(m--)
        {
            scanf("%lf%lf",&xi,&yi);
            pp=Point(xi,yi);
            int left=0,right=n,mid,tmp;
            while(left<=right)
            {
                mid=(left+right)/2;
                if(ccw(Line[mid].s,Line[mid].e,pp)==1)
                {
                    left=mid+1;
                }
                else if(ccw(Line[mid].s,Line[mid].e,pp)==-1)
                {
                    tmp=mid;
                    right=mid-1;
                }
            }
            ans[tmp]++;
        }
        printf("Box\n");
        int num[maxn];
        for(int i=1;i<=n;i++)
            num[i]=0;
        for(int i=0;i<=n;i++)
        {
            if(ans[i]>0)
             num[ans[i]]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(num[i]>0)
             printf("%d: %d\n",i,num[i]);
        }
    }
    return 0;
}
/*
4 4 1 5 10 2
2 3
5 4
6 7
9 8

2 2
3 3
4 4
5 3
*/