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

题意:

给定一个如上的长方形箱子,中间有n条线段,将其分为n+1个区域,给定m个玩具的坐标,统计每个区域中的玩具个数。(其中这些线段有序且不相交)

解答:

因为线段是有序给出,所以不用排序,判断某个点在哪个区域,采用二分法,将某个点和线段的叉积来判断这个点是在线的左边或者右边,根据这个来二分找出区域。

这是第一道计算几何的题目,怎么说呢,对于二分的边界问题还有点搞不清楚,这次是对线段二分,感觉二分真的很有用处。

开阔思维!!!具体请看代码。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
#define eps 1e-9
typedef long long ll;
using namespace std;
int n,m,x,y,x2,y2,temp;
int a[][],point[][],sum[]; void er(int l,int r,int keyx,int keyy)
{
ll X1,Y1,X2,Y2;
int mid;
while(l<r)
{
mid=l+(r-l)/;
X1=a[mid][]-keyx;
Y1=y-keyy;
X2=a[mid][]-keyx;
Y2=y2-keyy;
if(X1*Y2-X2*Y1<=)
r=mid;
else l=mid+;
}
sum[l]++;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n!=)
{
scanf("%d%d%d%d%d",&m,&x,&y,&x2,&y2);
for(int i=; i<n; i++)
scanf("%d%d",&a[i][],&a[i][]);
a[n][]=a[n][]=x2;//保证所有点都能找到叉积<=0的线段
for(int i=; i<m; i++)
{
scanf("%d%d",&point[i][],&point[i][]);
}
memset(sum,,sizeof(sum));
for(int i=; i<m; i++)
{
er(,n,point[i][],point[i][]);
}
for(int i=; i<=n; i++)
{
printf("%d: %d\n",i,sum[i]);
}
cout<<endl;
}
return ;
}

大神写的规范代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; struct Point {
int x, y;
}; struct Line {
Point a, b;
} line[]; int cnt[]; int Multi(Point p1, Point p2, Point p0) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
} void BSearch(Point a, int n) {
int l, r, mid; l = ; r = n-;
while (l < r) {
mid = (l + r) >> ;
if (Multi(a, line[mid].a, line[mid].b) > ) l = mid + ;
else r = mid;
}
if (Multi(a, line[l].a, line[l].b) < ) cnt[l]++;
else cnt[l+]++;
} int main()
{
int n, m, x1, y1, x2, y2;
int i, t1, t2;
Point a; while (scanf ("%d", &n) && n) {
scanf ("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
for (i = ; i < n; i++) {
scanf ("%d%d", &t1, &t2);
line[i].a.x = t1;
line[i].a.y = y1;
line[i].b.x = t2;
line[i].b.y = y2;
}
memset(cnt, , sizeof (cnt));
for (i = ; i < m; i++) {
scanf ("%d%d", &a.x, &a.y);
BSearch(a, n);
}
for (i = ; i <= n; i++)
printf ("%d: %d\n", i, cnt[i]);
printf("\n");
}
return ;
}

POJ2398:

本题和poj2318 TOYS大致一样,但有一些不同。

1:输入是无序的,需要自己排序。

2:输出是输出拥有相同玩具数的区块有几个。     (区块里的玩具数:相同玩具数的区块个数)

知道题意就很好做了。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
#define eps 1e-9
typedef long long ll;
using namespace std;
int n,m,x,y,x2,y2,temp;
int point[][],sum[];
struct node
{
int a1,a2;
} a[];
int cmp(const void *a,const void *b)
{
struct node *aa=(struct node *)a;
struct node *bb=(struct node *)b;
return aa->a1-bb->a1;
}
void er(int l,int r,int keyx,int keyy)
{
ll X1,Y1,X2,Y2;
int mid;
while(l<r)
{
mid=l+(r-l)/;
X1=a[mid].a1-keyx;
Y1=y-keyy;
X2=a[mid].a2-keyx;
Y2=y2-keyy;
if(X1*Y2-X2*Y1<=)
r=mid;
else l=mid+;
}
sum[l]++;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n!=)
{
scanf("%d%d%d%d%d",&m,&x,&y,&x2,&y2);
for(int i=; i<n; i++)
scanf("%d%d",&a[i].a1,&a[i].a2);
a[n].a1=a[n].a2=x2;
qsort(a,n+,sizeof(a[]),cmp);
for(int i=; i<m; i++)
{
scanf("%d%d",&point[i][],&point[i][]);
}
memset(sum,,sizeof(sum));
for(int i=; i<m; i++)
{
er(,n,point[i][],point[i][]);
}
printf("Box\n");
int ha[];
memset(ha,,sizeof(ha));
for(int i=; i<=m; i++)
{
if(sum[i])
ha[sum[i]]++;
}
for(int i=; i<=; i++)
{
if(ha[i]) printf("%d: %d\n",i,ha[i]);
}
}
return ;
}

POJ2318:TOYS(叉积判断点和线段的关系+二分)&&POJ2398Toy Storage的更多相关文章

  1. POJ 2398 Toy Storage (叉积判断点和线段的关系)

    题目链接 Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4104   Accepted: 2433 ...

  2. POJ 2398 Toy Storage(计算几何,叉积判断点和线段的关系)

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3146   Accepted: 1798 Descr ...

  3. POJ2318 TOYS(叉积判断点与直线的关系+二分)

    Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a prob ...

  4. poj2318(叉积判断点在直线左右+二分)

    题目链接:https://vjudge.net/problem/POJ-2318 题意:有n条线将矩形分成n+1块,m个点落在矩形内,求每一块点的个数. 思路: 最近开始肝计算几何,之前的几何题基本处 ...

  5. POJ2318 TOYS[叉积 二分]

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14433   Accepted: 6998 Description ...

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

    解题关键:计算几何入门题,通过叉积判断. 两个向量的关系: P*Q>0,Q在P的逆时针方向: P*Q<0,Q在P的顺时针方向: P*Q==0,Q与P共线. 实际就是用右手定则判断的. #i ...

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

    题目链接:https://cn.vjudge.net/problem/POJ-2318 题意 在一个矩形内,给出n-1条线段,把矩形分成n快四边形 问某些点在那个四边形内 思路 二分+判断点与位置关系 ...

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

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

  9. poj 2318(叉积判断点在线段的哪一侧)

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13120   Accepted: 6334 Description ...

随机推荐

  1. KMP + 求最小循环节 --- HUST 1010 - The Minimum Length

    The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...

  2. MyBatis 最强大的特性之一就是它的动态语句功能

    MyBatis 最强大的特性之一就是它的动态语句功能.如果您以前有使用JDBC或者类似框架的经历,您就会明白把SQL语句条件连接在一起是多么的痛苦,要确保不能忘记空格或者不要在columns列后面省略 ...

  3. "reason":"No handler for type [attachment] declared on field [file]" 最完全解决方案

    0.elasticsearch-mapper-attachments 2.3.4安装 mapper-attachments安装方法分两类,在线和离线: 在线安装 bin/elasticsearch-p ...

  4. 去掉if

    修改前 namespace CleanCSharp.Methods.Dirty {     class BooleanSwitchingArgumentsExample     {         p ...

  5. Java去除所有非中文字符串

    "fdsfjasd阿斯顿飞机阿斯蒂芬,,,,,,,,....".replaceAll("[^\u4E00-\u9FA5]", "");

  6. node 下好用的工具

    1. supervisor Node Supervisor is used to restart programs when they crash. Node Supervisor 是用来当程序崩溃时 ...

  7. CodeForces 42C Safe cracking 规律题

    题目链接:点击打开链接 3个数为一组,找最大的一个数让它降低,则显然是有解的,分类讨论一下就可以 #include<cstdio> #include<cstring> #inc ...

  8. HYSBZ 2243(染色)

    题目链接:传送门 题目大意:中文题,略 题目思路:树链剖分,区间更新,区间查询. 闲谈:      只想说这道题做的好苦逼..去长春现场赛之前就没A,回来后又做了2天才A掉,蒟蒻太菜了 这道题也没有想 ...

  9. List remove及ConcurrentModificationException异常

    参考:http://blog.csdn.net/androidboy365/article/details/50540202/ 解决方案 // 1 使用Iterator提供的remove方法,用于删除 ...

  10. Leetcode-Bianry Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...