POJ 2398--toy Storage (cross product judgment, two points to find, point sorting)

Source: Internet
Author: User

Toy Storage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6534 Accepted: 3905

Description

Mom and Dad had a problem:their child, Reza, never puts his toys away when he was finished playing with them. They gave Reza a rectangular box to put he toys in. Unfortunately, Reza is rebellious and obeys he parents by simply throwing he toys into the box. All the toys get mixed up, and it's 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 to the box, at least toys, then get thrown into different partitions stay separate. The box looks like this from the top:

We want for each positive integer t, such this there exists a partition with T-toys, determine how many partitions has 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 <=) and the number of the toys are 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 both integers Ui Li, indicating that the ends of the ith cardboard are at the Coordin Ates (Ui, y1) and (Li, y2). You may assume that the cardboards does not be intersect with each of the other. The next m lines each consists of the integers Xi Yi specifying where the ith toy have landed in the box. Assume that no toy would land on a cardboard.

A line consisting of a single 0 terminates the input.

Output

For each box, first provide a header stating "box" to a line of its own. After that, there would be the one line of output per count (T > 0) of the toys in a partition. The value T is followed by a colon and a space, followed the number of partitions containing T toys. Output'll is sorted in ascending order of T for each box.

Sample Input

4 10 0 10 100 020 2080 8060 6040 405 1015 1095 1025 1065 1075 1035 1045 1055 1085 105 6 0 10 60 04 315 303 16 810 102 12 8 1 55 540) 107 90

Sample Output

Box2:5box1:42:1

Source

Tehran 2003 Preliminary
  • Test instructions: given n order-free edges, dividing a rectangle into n+1 regions, and then given M points, to find out how many points each region has, the output will be sorted in ascending order by the number of points that exist in the region.
    Similar to POJ2318, but the points here need to be sorted, and the output will be different.
  • Code
    1#include <iostream>2#include <cstring>3#include <string>4#include <algorithm>5#include <cstdio>6#include <cstdlib>7#include <cmath>8 using namespacestd;9 Const intMAX =5005;TentypedefstructPoint { One     intx; A     inty; - }point; -typedefstructValue { the Point start; - Point end; - }v; - v Edge[max]; + intSum[max], Ans[max]; - intN, M, x1, Y11, x2, y2, flag =1, Ui, Li; + Point tp; A intXj, Yj; at BOOLcomConstV T1,Constv T2) { -     returnT1.start.x <T2.start.x; - } - BOOLCOM2 (Const intAConst intb) { -     returnA <b; - } in intmulti (point P1, point P2, point p0) {//Judging the relationship between P1p0 and P2p0, <0,p1p0 in the counterclockwise direction of P2p0 -     return(p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (P1.Y-p0.y); to } + voidinset (point p) { -     intLow =0, high =N; the      while(Low <=High ) { *         intMid = (high + low)/2; $         if(multi (p, Edge[mid].start, Edge[mid].end) <0)/*Point P1 on the left side of the edge*/Panax NotoginsengHigh = mid-1; -         Else    //point P on the right side of the edge theLow = mid +1; +     } A     if(multi (p, edge[low-1].start, edge[low-1].end) <0 ) thesum[low-1]++; +     Else -sum[low]++; $ } $ intMain () { -      while(Cin>>n &&N) { -memset (SUM,0,sizeof(sum)); thememset (ans,0,sizeof(ans)); -CIN >> m >> x1 >> y11 >> x2 >>Y2;Wuyi          for(inti =0; I < n; i++) { theCIN >> Ui >>Li; -Edge[i].start.x =Ui; WuEdge[i].start.y =Y11; -Edge[i].end.x =Li; AboutEdge[i].end.y =Y2; $         } -Edge[n].start.x =x2; -Edge[n].start.y =Y11; -Edge[n].end.x =x2; AEdge[n].end.y =Y2; +Sort (edge, Edge + n +1, com); the          for(intj =0; J < M; J + +) { -CIN >> Xj >>Yj; $tp.x =Xj; theTP.Y =Yj; the inset (TP); the         } the          for(inti =0; I <= N; i++) -         { in             if(Sum[i]! =0) theans[sum[i]]++; the         } Aboutcout <<"Box"<<Endl; the          for(inti =0; I <= N; i++) the         { the             if(Ans[i]! =0) +cout << i <<": "<< Ans[i] <<Endl; -         }     the     }Bayi     return 0; the}
    View Code
  • Get acquainted with the cross-product function again
    1 Get acquainted with the cross-product function again 2 int  3     return (p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (P1.Y- p0.y); 4 }5  // Judging the relationship between P1p0 and P2p0 6  // Results < 0, p1p0 in p2p0 counterclockwise direction, point P1 on the left side of P2p0 7  // result >0, p1p0 in p2p0 clockwise direction, point P1 on the right side of p2p0 
    View Code

POJ 2398--toy Storage (cross product judgment, two points to find, point sorting)

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.