• 寻找区域中有几个点 叉乘+二分 poj 2318


    题目来源:http://poj.org/problem?id=2318

    一个 矩阵 被分成多个 区域, 然后输入 多个点, 输出 每个区域点的 个数。 当寻找点 落在某个区域时, 用二分法。

    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #include <iostream>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const int N =6000;
    const double PI = 3.1415927;
    
    struct Point{
        int x,y;
        Point(){}
        Point(int x,int y):x(x),y(y){} // 构造函数,方便代码编写
        Point(const Point & p):x(p.x),y(p.y){}
        Point operator +(Point p){
            return Point(x+p.x,y+p.y);
        }
        Point operator-(Point p){
            return Point(x-p.x,y-p.y);
        }
        Point operator*(double d){
            return Point(x*d,y*d);
        }
        int operator*(Point p){  // 内积 点乘
            return x*p.x+ y*p.y;
        }
        int operator^(Point p){//  外积 叉乘
            return x*p.y-y*p.x;
        }
        friend ostream& operator<<(ostream& os,const Point& p ){
            os<<p.x<<" "<<p.y<<endl;
            return os;
        }
        friend istream& operator>>(istream& is, Point& p) {// Point 不能是常量,是变量
            is>>p.x>>p.y;
            return is;
        }
    };
    Point up[N];
    Point low[N];
    int num[N];
    int n;
    void bin_search(Point p)   // 二分法,寻找点所在的区域,用叉乘,注意结束条件是 left+1== right
    {
        int left=0,mid;
        int right = n+1;
        while(left<=right)
        {
            mid=(left+right) /2;
            int t=(up[mid]-low[mid])^(p-low[mid]);
            if( t >0  )
                right=mid;
            else if( t <0 )
                left=mid;
            if(left+1 == right )
            {
                num[left]++;
                 break;
            }
        }
    }
    int main() {
        int m;
        int a,b,count=0;
        Point p;
        while(scanf("%d",&n)!=EOF && n)
        {
            if(count) printf("
    ");
            count=1;
            scanf("%d",&m);
            scanf("%d%d%d%d",&up[0].x,&up[0].y,&low[n+1].x,&low[n+1].y);
            low[0].x=up[0].x;
            up[n+1].x=low[n+1].x;
            for(int i=0;i<=n+1;i++)
            {
                up[i].y=up[0].y;
                low[i].y=low[n+1].y;
            }
            for(int i=1;i<=n;i++)
            {
                scanf("%d%d",&a,&b);
                up[i].x=a;
                low[i].x=b;
            }
            memset(num,0,sizeof(num));
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d",&p.x,&p.y);
                bin_search(p);
            }
            for(int i=0;i<=n;i++)
                printf("%d: %d
    ",i,num[i]);
        }
        return 0;
    }
  • 相关阅读:
    Java Class/Method
    Data Types in Java
    Java overview && JVM
    Interface Vs. Abstract Class
    【javascript】Object三种定义方式
    【C#】导出Excel
    【javascript】简单原型链、借用构造函数
    Mysql表编码查看修改
    asp.net的mvc?
    连表
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3623613.html
Copyright © 2020-2023  润新知