POJ2318 TOYS (计算几何)

### 题目链接 ###

题目大意:
给定一个矩形,这个矩形被 \(n\) 条线段(端点分别在矩形上下边界上)切割成 \(n+1\) 个块。再给你 \(m\) 个物品的坐标,问每个块中会有多少个物品,保证物品放置位置合法。

分析:

一个物品在一个块中时,他一定夹在两个分界线中间,且在左分界线的右边,右分界线的左边。故按图中那样,将物品连接某个边界下端,构成一个向量,再判断 蓝色向量 与 紫色向量 的位置关系,进行叉积即可。故二分找到第一个 物品向量 \(×\) 边界向量 为负的位置,即为该物品所在块的右边界(或者二分最后一个叉积为正的)。

代码如下:

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define first fi
#define second se
#define IO std::ios::sync_with_stdio(false)
typedef long long ll;
const int maxn = 100008;
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18 + 10;
const double eps = 1e-6;
using namespace std;
int n,m;
ll x1,y1,x2,y2;
struct Node{
    ll x,xx;
}nd[maxn];
struct Goods{
    ll x,y;
}a[maxn];
int sum[maxn];
bool judge(int i,int j){
    Goods e1,e2;
    e1.x=nd[j].x-nd[j].xx,e1.y=y1-y2;
    e2.x=a[i].x-nd[j].xx,e2.y=a[i].y-y2;
    ll ans=e2.x*e1.y-e2.y*e1.x;
    return ans>0;
}
int main()
{
    while(~scanf("%d",&n)&&n){
        scanf("%d%lld%lld%lld%lld",&m,&x1,&y1,&x2,&y2);
        memset(sum,0,sizeof(sum));
        nd[0].x=nd[0].xx=x1;
        nd[n+1].x=nd[n+1].xx=x2;
        for(int i=1;i<=n;i++) scanf("%lld%lld",&nd[i].x,&nd[i].xx);
        for(int i=1;i<=m;i++) scanf("%lld%lld",&a[i].x,&a[i].y);
        for(int i=1;i<=m;i++){
            int l=0,r=n+1,mid;
            while(l<=r){
                mid=(l+r)/2;
                if(judge(i,mid)) l=mid+1;
                else r=mid-1;
            }
            sum[r]++;
        }
        for(int i=0;i<=n;i++) printf("%d: %d\n",i,sum[i] );
        putchar('\n');
    }
}

原文地址:https://www.cnblogs.com/Absofuckinglutely/p/12442772.html

时间: 2024-10-28 19:49:01

POJ2318 TOYS (计算几何)的相关文章

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】2318 TOYS ——计算几何+二分

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

poj2318 TOYS 【计算几何】【点和线的关系】

题目链接:http://poj.org/problem?id=2318 题目大意:给你n,m,x1,y1,x2,y2表示的分别是n个线,m个点,(x1,y1)表示的是矩形左上角那个点,(x2,y2)表示的是矩形右下角那个点. 然后给出n个线(L)的x坐标L.s.x,L.e.x,就相当于是给出了线的位置,下面给出m个点的坐标. 最后问n条线分成的n+1个区域内各有多少个点. 题目不是很难,不过与二分结合起来还是有点意思的. 注意叉乘的性质,在什么时候叉乘的结果会是小于0什么时候会是大于0. 如果是

POJ2318 TOYS[叉积 二分]

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 w

poj 2318 TOYS(计算几何 点与线段的关系)

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

[POJ2318]TOYS

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

POJ2318(KB13-A 计算几何)

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

[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; typede

TOYS(计算几何-入门)

题目 '^'代表叉乘 '?'代表点乘 点积:a?b=ax*bx+ay*by 叉积:a^b=ax*by-bx*ay 有了这些,代码就呼之欲出了. 首先read(线段) 然后read(点) 发现满足二分性(点A一定在前t条线后,在后n-t条线前) 于是,二分优化查找 O(mlogn) #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include &l