POJ 2318 TOYS(计算几何)

跨产品的利用率推断点线段向左或向右,然后你可以2分钟

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 5005;

int n, m, x1, y1, x2, y2;

struct Point {
    int x, y;
    Point() {}
    Point(int x, int y) {
        this->x = x;
        this->y = y;
    }
};

typedef Point Vector;

Vector operator - (Vector A, Vector B) {
    return Vector(A.x - B.x, A.y - B.y);
}

struct Seg {
    Point a, b;
    Seg() {}
    Seg(Point a, Point b) {
        this->a = a;
        this->b = b;
    }
} seg[N];

int ans[N];

int Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;}

void gao(Point p) {
    int l = 0, r = n;
    while (l < r) {
        int mid = (l + r) / 2;
        if (Cross(seg[mid].a - p, seg[mid].b - p) < 0) r = mid;
        else l = mid + 1;
    }
    ans[l]++;
}

int main() {
    while (~scanf("%d", &n) && n) {
        memset(ans, 0, sizeof(ans));
        scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
        int x, y;
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &x, &y);
            seg[i] = Seg(Point(x, y1), Point(y, y2));
        }
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &x, &y);
            gao(Point(x, y));
        }
        for (int i = 0; i <= n; i++)
            printf("%d: %d\n", i, ans[i]);
        printf("\n");
    }
    return 0;
}
时间: 2024-10-26 00:54:45

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

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

【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

POJ 2318 TOYS(叉积+二分or暴力)

题目链接:POJ 2318 TOYS [写在前面]前几天跟队友分了方向,学渣开始进行计算几何的专题了,真是脑壳有点痛啊.但是我想做多了就没这么坑爹了 [题意]大体意思就是给你一个矩形,有被若干直线分成N个格子,给出M个点的坐标,问你每个点位于哪个格子中. [思路]其实就是点在凸四边形内的判断,然后就可以利用叉积的性质,当然可以用暴力枚举也可以过,但是时间复杂度有点高,最好是用二分求解.(一直觉得二分真是牛逼啊) 下面贴AC代码,用二分219MS就过了: 1 /* 2 ** POJ 2318 TO

poj 2318 TOYS &amp; poj 2398 Toy Storage (叉积)

链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段,并且这些线段坐标是按照顺序给出的, 有n条线段,把盒子分层了n+1个区域,然后有m个玩具,这m个玩具的坐标是已知的,问最后每个区域有多少个玩具 分析:从左往右,直到判断玩具是否在线段的逆时针方向为止,这个就需要用到叉积,当然可以用二分查找优化. 叉积:已知向量a(x1,y1),向量b(x2,y2),axb=x1*y2-x2*y1, 若axb>0,a在b的逆时针方向,若axb<0,则a在b的顺时针方向 注:每组数据后要多空一行

POJ 2318 TOYS 叉积的应用

A - TOYS Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2318 Appoint description:  lijunle  (2011-07-18)System Crawler  (2016-05-08) Description Calculate the number of toys that land in each b

poj 2318 TOYS(计算几何)

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

TOYS - POJ 2318(计算几何,叉积判断)

题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数. 分析:做的第一道计算几何题目....使用叉积判断方向,然后使用二分查询找到点所在的区域. 代码如下: ============================================================================================================================ #

poj 2318 TOYS 2012-01-11

http://poj.org/problem?id=2318 ___________________________________________________ 题目大意:一个箱子用木板分成几个区间,给一些玩具的坐标,求各个区间有几个玩具. 对于每一个玩具,因为输入的区间是有序的,所以采用二分木板,判断一个玩具在哪个区间. ___________________________________________________ 1 Program Stone; 2 var i,n,m,x1,x

POJ 2318 TOYS 叉积

题意: 给出一个矩形范围,给出n条线段,这n条线段一定与矩形上下边界相交且互不相交,将矩形分成n+1个划分.给出m个玩具的坐标.求每个划分放的玩具数,玩具保证不会在线段和左右边界上. 分析: 判断点是否在两条直线中间,利用叉积,如果在两条直线间,必定会有两个叉积一个小于0,一个大于0(不能把相乘小于0作为判断条件) #include <iostream> #include <cstdio> #include <cstring> using namespace std;