码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2318 TOYS[叉积 二分]

时间:2017-01-26 23:17:39      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:output   mos   lang   accept   sizeof   lines   ges   cardboard   dea   

TOYS
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14433   Accepted: 6998

Description

Calculate the number of toys that land in each bin of a partitioned toy box. 
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John 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 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 into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box. 
技术分享 
For this problem, you are asked to determine how 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 m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) 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 two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, 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 no toy will 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 will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.

题意:一些直线把一个矩形分成n+1份,给你m个点问每个点在哪一份里

用叉积判断左右关系,二分即可
//
//  main.cpp
//  poj2318
//
//  Created by Candy on 2017/1/26.
//  Copyright © 2017年 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=5005;
const double eps=1e-8;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<0||c>9){if(c==-)f=-1; c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0; c=getchar();}
    return x*f;
}
inline int sgn(double x){
    if(abs(x)<eps) return 0;
    else return x<0?-1:1;
}
struct Vector{
    double x,y;
    Vector(double a=0,double b=0):x(a),y(b){}
    bool operator <(const Vector &a)const{
        return x<a.x||(x==a.x&&y<a.y);
    }
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}

double Cross(Vector a,Vector b){
    return a.x*b.y-a.y*b.x;
}

struct Line{
    Point p;
    Vector v;
    Line(){}
    Line(Point p,Vector v):p(p),v(v){}
    bool operator <(const Line a)const{
        return sgn(Cross(v,a.v))>=0;
    }
}a[N];
bool OnLeft(Line l,Point p){
    return sgn(Cross(l.v,p-l.p))>=0;
}

int n,m,x,y,x2,y2,u,l,xx,yy;
int ans[N];
int main(int argc, const char * argv[]) {
    while(true){
        memset(ans,0,sizeof(ans));
        n=read();if(n==0) break;
        m=read();x=read();y=read();x2=read();y2=read();
        for(int i=1;i<=n;i++) u=read(),l=read(),a[i]=Line(Point(l,y2),Vector(u-l,y-y2));
        a[++n]=Line(Point(x2,y2),Vector(0,y-y2));
        for(int i=1;i<=m;i++){
            x=read();y=read();
            Point p=Point(x,y);
            int l=1,r=n,pos=-1;
            while(l<=r){
                int mid=(l+r)>>1;
                if(OnLeft(a[mid],p)) pos=mid,r=mid-1;
                else l=mid+1;
            }
            ans[pos]++;
        }
        for(int i=1;i<=n;i++) printf("%d: %d\n",i-1,ans[i]);
        puts("");
    }

    return 0;
}

 

 

POJ2318 TOYS[叉积 二分]

标签:output   mos   lang   accept   sizeof   lines   ges   cardboard   dea   

原文地址:http://www.cnblogs.com/candy99/p/6352162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!