[poj2318]TOYS(直线与点的位置关系)

解题关键:计算几何入门题,通过叉积判断。

两个向量的关系:

P*Q>0,Q在P的逆时针方向;

P*Q<0,Q在P的顺时针方向;

P*Q==0,Q与P共线。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
struct point{
    int x,y;
};
int n,m,x1,x2,y11,y2;
struct Line{
    point a,b;
}A[5010];
double operator*(point p1,point p2){return p1.x*p2.y-p2.x*p1.y;}
point operator-(point A,point B){return {A.x-B.x,A.y-B.y};}
int pos[5010];
bool judge(point t,int mid){//叉积
    point tt=A[mid].b-A[mid].a;
    int ans=tt*(t-A[mid].a);
    return ans<=0;
}

int erfen(point xx){
    int l=0,r=n;
    while(l<r){
        int mid=(l+r)>>1;
        if(judge(xx,mid))  r=mid;
        else l=mid+1;
    }
    return r;
}

int main(){
    while(scanf("%d",&n)!=EOF&&n){
        scanf("%d%d%d%d%d",&m,&x1,&y11,&x2,&y2);
        for(int i=0;i<n;i++){
            int xd,xu;
            scanf("%d%d",&xu,&xd);
            A[i]={{xu,y11},{xd,y2}};
        }
        memset(pos,0,sizeof pos);
        for(int i=0;i<m;i++){
            int xx,yy;
            scanf("%d%d",&xx,&yy);
            point t={xx,yy};
            int ans=erfen(t);
            pos[ans]++;
        }
        for(int i=0;i<=n;i++){
            printf("%d: %d\n",i,pos[i]);
        }
        printf("\n");
    }
    return 0;
}  

原文地址:https://www.cnblogs.com/elpsycongroo/p/8723586.html

时间: 2024-08-09 10:06:58

[poj2318]TOYS(直线与点的位置关系)的相关文章

poj 2318 TOYS (点与线段位置关系判断)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10848   Accepted: 5206 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 w

Segments---poj3304(判断直线与线段的位置关系)

题目链接:http://poj.org/problem?id=3304 题意:给你n个线段,求是否有一条直线与所有的线段都相交,有Yes,没有No; 枚举所有的顶点作为直线的两点,然后判断这条直线是否和所有的线段相交即可;注意不能找两个相同的点作为直线上的两点: #include<iostream> #include<algorithm> #include<math.h> #include<string.h> #include<stdio.h>

POJ1269_Intersecting Lines(几何/叉积判断直线位置关系)

解题报告 题目传送门 题意: 判断直线的位置关系(平行,重合,相交) 思路: 两直线可以用叉积来判断位置关系. AB直线和CD直线 平行的话端点C和端点D会在直线AB的同一侧. 重合的话在直线AB上. 剩下就是相交. 求两直线交点可以用面积比和边长比来求. 看下面的图就知道了,推导就比较容易了 #include <iostream> #include <cstring> #include <cstdio> #define eps 1e-6 #define zero(x)

Intersecting Lines---poj1269(求两直线的位置关系)

题目链接:http://poj.org/problem?id=1269 题意:给你两条直线上的任意不同的两点,然后求两条直线的位置关系,如果相交于一点输出该点坐标; #include<iostream> #include<algorithm> #include<math.h> #include<string.h> #include<stdio.h> #include<map> #include<vector> #inclu

[从头学数学] 第171节 点、直线、平面之间的位置关系

剧情提要: [机器小伟]在[工程师阿伟]的陪同下进入了结丹初期的修炼, 这次要修炼的目标是[点.直线.平面之间的位置关系]. 正剧开始: 星历2016年04月10日 13:48:09, 银河系厄尔斯星球中华帝国江南行省. [工程师阿伟]正在和[机器小伟]一起研究[点.直线.平面之间的位置关系]. 来画个平面玩玩吧. <span style="font-size:18px;">//平面 if (1) { var r = 20; config.setSector(1,1,1,1

判断两条直线的位置关系 POJ 1269 Intersecting Lines

两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, p2, p3, p4,直线L1,L2分别穿过前两个和后两个点.来判断直线L1和L2的关系 这三种关系一个一个来看: 1. 共线. 如果两条直线共线的话,那么另外一条直线上的点一定在这一条直线上.所以p3在p1p2上,所以用get_direction(p1, p2, p3)来判断p3相对于p1p2的关

POJ-2318 TOYS 计算几何 判断点在线段的位置

题目链接:https://cn.vjudge.net/problem/POJ-2318 题意 在一个矩形内,给出n-1条线段,把矩形分成n快四边形 问某些点在那个四边形内 思路 二分+判断点与位置关系 提交过程 WA*n x1和x2,y1和y2在复制的时候没分清(哭 WA 可能存在二分问题? AC 代码 #define PI 3.1415926 #include <cmath> #include <cstdio> #include <vector> #include &

POJ 1269 Intersecting Lines (判断直线位置关系)

题目链接:POJ 1269 Problem Description We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line becau

LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边形的内部; 射线法判断即可适用于任何(凸或凹)多边形;时间复杂度为O(n); 判断一个点是在多边形内部,边上还是在外部,时间复杂度为O(n):射线法可以正确用于凹多边形: 射线法是使用最广泛的算法,这是由于相比较其他算法而言,它不但可以正确使用在凹多边形上,而且不需要考虑精度误差问题.该算法思想是从