Application of POJ 2318TOYS cross product

Source: Internet
Author: User

A-TOYSTime limit:$ MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 2318Appoint Description:Lijunle (2011-07-18)System Crawler (2016-05-08)

Description

Calculate the number of toys that land in each bin of a partitioned toy box.
Mom and Dad had a problem-their child John never puts his toys away when he was finished playing with them. They gave John a rectangular box to put he toys in, but John is rebellious and obeys he parents by simply throwing he t Oys into the box. All the toys get mixed up, and it's 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 to the box, at least toys, then get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.

For this problem, you is asked to determine what 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 mX1Y1X2y2. The number of cardboard partitions is n (0 < n <=) 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 and the integers per line,UiLi, indicating that the ends of thei-thCardboard partition is at the coordinates (Ui,Y1) and (Li,y2). Assume that the cardboard partitions does not intersect each other and that they is specified in sorted order from Left-to-right. The next m lines contain, integers per line,Xj Yjspecifying where thej-thToy have landed in the box. The order of the toy locations is random. Assume that no toy would 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 would be a line for each separate bin in the Toy box. For each bin, print it bin number, followed by a colon and one space, followed by the number of the toys thrown into the bin . Bins is 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 03 14 36 810 1015 301 52 12 85 540 107 94 10 0 10 100 020 2040 4060 6080 80 5 1015 1025 1035 1045 1055 1065 10 75 1085 1095 100

Sample Output

0:21:12:13:14:05:10:21:22:23:2

4:2

Give you n lines to divide a rectangle into a n+1 space.

I'll give you the M-point to tell you what space the M points are in and how they count.

At first, I wanted to be more complicated, trying to determine if the point was in this n+1 4-edged shape.

It turns out that just finding the number of the nearest line to the right of each point determines which space the point belongs to.

Here we use the knowledge of the cross product for vectors p and q

P*q>0 p in Q clockwise direction

P*q<0 p in the Q counter-clockwise direction

P*q=0 p and Q collinear is not necessarily the same

The connection point P and the point below each segment form a vector that can be used to determine the position of a point for different segments

Here we take a two-point segment to determine the area of the dot

Accode:

#include <cstring> #include <cstdio> #define MAXN 5050struct point{int x, y;        Point () {}, point (int _x,int _y) {x=_x;    y=_y;    }}p[maxn];struct line{Point S,e;        Line () {} line (point _s,point _e) {s=_s;    E=_e; }}my[maxn];int ans[maxn];int multi (point p1,point p2,point p0) {return (p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y );}    int main () {int n,m,x1,y1,x2,y2;        while (~SCANF ("%d", &n), N) {scanf ("%d%d%d%d%d", &m,&x1,&y1,&x2,&y2);            for (int i=0;i<n;++i) {int A, B;            scanf ("%d%d", &a,&b);        My[i]=line (Point (A,y1), point (B,y2));        } my[n]=line (Point (X2,y1), point (X2,y2));        memset (ans,0,sizeof (ans));            for (int i=1;i<=m;++i) {int x, y;            scanf ("%d%d", &x,&y);            Point Tmp=point (x, y);            int l=0,r=n,id;                while (l<=r) {int mid= (L+R) >>1; if (multi (my[MID].S,MY[MID].E,TMP) <0) {id=mid;                R=mid-1;            } else l=mid+1;        } ans[id]++;        } for (int i=0;i<=n;++i) printf ("%d:%d\n", i,ans[i]);    Putchar (' \ n '); } return 0;}


Application of POJ 2318TOYS cross product

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.