POJ2398 POJ2318的加入排序版

来源:互联网 发布:旅游电商分销系统源码 编辑:程序博客网 时间:2024/05/22 00:19

题目大意: 和POJ2318几乎一模一样,2318题目可以见我博客下面两篇……只是给的隔板没有排序,因为题目给出隔板不会相交,所以对x排序即可,我是直接用了sort函数……没用二分查找,0ms过……我也不知道为什么,数据太弱了? 还有就是他统计的是对于所有隔板区间有m个球的区间个数是多少,这和2318统计的每个隔板有多少球不一样……在最后加一个统计就好。long long 类型吧,int可能会超……


AC代码:

#include <cstdio>#include <cstring>#include<vector>#include <algorithm>#include <iostream>#include <climits>#include <numeric>#include<cmath>#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)#define REP(i,n) for(int i=0;i<n;++i)using namespace std;const double EPS = 1e-8;inline int sign(double a){    return a < -EPS ? -1 : a > EPS;}struct Point{    double x, y;    Point()    {    }    Point(double _x, double _y) :        x(_x), y(_y)    {    }    Point operator+(const Point&p) const    {        return Point(x + p.x, y + p.y);    }    Point operator-(const Point&p) const    {        return Point(x - p.x, y - p.y);    }    Point operator*(double d) const    {        return Point(x * d, y * d);    }    Point operator/(double d) const    {        return Point(x / d, y / d);    }    bool operator<(const Point&p) const    {        int c = sign(x - p.x);        if (c)            return c == -1;        return sign(y - p.y) == -1;    }    double dot(const Point&p) const    {        return x * p.x + y * p.y;    }    double det(const Point&p) const    {        return x * p.y - y * p.x;    }    double alpha() const    {        return atan2(y, x);    }    double distTo(const Point&p) const    {        double dx = x - p.x, dy = y - p.y;        return hypot(dx, dy);    }    double alphaTo(const Point&p) const    {        double dx = x - p.x, dy = y - p.y;        return atan2(dy, dx);    }    void read()    {        scanf("%lf%lf", &x, &y);    }    double abs()    {        return hypot(x, y);    }    double abs2()    {        return x * x + y * y;    }    void write()    {        cout << "(" << x << "," << y << ")" << endl;    }};#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))Point isSS(Point p1, Point p2, Point q1, Point q2)       //可求p1,p2 直线与q1,q2的焦点。。不过不确定{    double a1 = cross(q1,q2,p1), a2 = -cross(q1,q2,p2);    Point temp;    temp.x=sign((p1.x*a2+p2.x*a1)/(a1+a2))==0?0:(p1.x*a2+p2.x*a1)/(a1+a2);    temp.y=sign((p1.y*a2+p2.y*a1)/(a1+a2))==0?0:(p1.y*a2+p2.y*a1)/(a1+a2);    return temp;}struct Border{    Point p1, p2;    long double alpha;    void setAlpha()    {        alpha = atan2(p2.y - p1.y, p2.x - p1.x);    }    void read()    {        p1.read();        p2.read();        setAlpha();    }};bool cmp(Border p,Border q){    return p.p1.x<q.p1.x;}int main(){    int n, m, x1, y1, x2, y2;    int t1, t2;    Point a;    Border line[5001];    int cnt[5002];long long cnt1[5002];    while (scanf ("%d", &m) && m)    {        scanf ("%d%d%d%d%d", &n, &x1, &y1, &x2, &y2);        for (int i = 0; i < m; i++)        {            scanf ("%d%d", &t1, &t2);            line[i].p1.x = t1;            line[i].p1.y = y1;            line[i].p2.x = t2;            line[i].p2.y = y2;        }        sort(line,line+m,cmp);        int flag=0;        memset(cnt, 0, sizeof (cnt));        memset(cnt1,0, sizeof(cnt1));        for(int i=1; i<=n; i++)        {            Point toy;            toy.read();             flag=0;            for(int j=0; j<m; j++)            {                if(crossOp(line[j].p1,toy,line[j].p2)>=0)                {                    flag=1;                    cnt[j]++;                    break;                }            } if(flag==0)            cnt[m]++;        }        printf("Box\n");        for(int i=0;i<=n;i++)        {            if(cnt[i])            cnt1[cnt[i]]++;        }        for(int i=1;i<=m;i++)        {            if(cnt1[i])            printf("%d: %lld\n",i,cnt1[i]);        }    }    return 0;}