poj2318

来源:互联网 发布:大自然音效软件 编辑:程序博客网 时间:2024/05/05 01:10

http://poj.org/problem?id=2318

TOYS
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 6790Accepted: 3213

Description

Calculate the numberof toys that land in each bin of a partitioned toy box.
Mom and dad have a problem - their child John never puts his toysaway when he is finished playing with them. They gave John arectangular box to put his toys in, but John is rebellious andobeys his parents by simply throwing his toys into the box. All thetoys get mixed up, and it is impossible for John to find hisfavorite toys.

John's parents came up with the following idea. They put cardboardpartitions into the box. Even if John keeps throwing his toys intothe box, at least toys that get thrown into different bins stayseparated. The following diagram shows a top view of an example toybox.
poj2318
For this problem, you are asked to determine how many toys fallinto each partition as John throws them into the toy box.

Input

The input filecontains one or more problems. The first line of a problem consistsof six integers, n m x1 y1 x2 y2. The number of cardboardpartitions is n (0 < n <= 5000) andthe number of toys is m (0 < m <=5000). The coordinates of the upper-left corner and the lower-rightcorner of the box are (x1,y1) and (x2,y2), respectively. Thefollowing n lines contain two integers per line, Ui Li, indicatingthat the ends of the i-th cardboard partition is at the coordinates(Ui,y1) and (Li,y2). You may assume that the cardboard partitionsdo not intersect each other and that they are specified in sortedorder from left to right. The next m lines contain two integers perline, Xj Yj specifying where the j-th toy has landed in the box.The order of the toy locations is random. You may assume that notoy will land exactly on a cardboard partition or outside theboundary of the box. The input is terminated by a line consistingof a single 0.

Output

The output for eachproblem will be one line for each separate bin in the toy box. Foreach bin, print its bin number, followed by a colon and one space,followed by the number of toys thrown into that bin. Bins arenumbered from 0 (the leftmost bin) to n (the rightmost bin).Separate the output of different problems by a single blankline.

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 1075 1085 1095 100

Sample Output

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

Hint

As the exampleillustrates, toys that fall on the boundary of the box are "in" thebox.

Source

Rocky Mountain 2003
AC代码如下
  • Source Code
    #include<stdio.h>#include<string.h>#define MAXD 5010int N, M, X1, Y1, X2, Y2, a[MAXD], b[MAXD], h[MAXD];long long int det(int x1, int y1, int x2, int y2){            return (long long int)x1 * y2 - (long long int)x2 * y1;}void init(){            int i, j, k;            a[0] = b[0] = X1;            for(i = 1; i <= N; i ++)                        scanf("%d%d", &a[i], &b[i]);}void solve(){            int i, j, k, x, y, mid, min, max;            memset(h, 0, sizeof(h[0]) * (N + 1));            for(i = 0; i < M; i ++)            {                        scanf("%d%d", &x, &y);                        min = 0, max = N + 1;                        for(;;)                       {                                    mid = (max - min) / 2 + min;                                    if(mid == min)                                               break;                                    if(det(a[mid] - b[mid], Y1 - Y2, x - b[mid], y - Y2) < 0)                                                min = mid;                                    else                                               max = mid;                        }                        ++ h[mid];            }            for(i = 0; i <= N; i ++)                        printf("%d: %d\n", i, h[i]);}int main(){            int t = 0;            while(scanf("%d%d%d%d%d%d", &N, &M, &X1, &Y1, &X2, &Y2) == 6)            {                        if(t ++)                                    printf("\n");                        init();                        solve();            }           return 0;}
  • 原创粉丝点击