hdu 4353 统计点在三角形内的个数
Finding Mine
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1120 Accepted Submission(s): 298
As a businessman, Bob wants to buy just a part of the area, which is a simple polygon, whose vertex can only be chosen from N points given in the input (a simple polygon is a polygon without self-intersection). As a greedy man, he wants to choose the part with a lot of gold mines, but unluckily, he is short with money.
Those M gold mines can also be seen as points, but they may be different from those N points. You may safely assume that there will be no three points lying on the same line for all N+M points.
Bob alreadys knows that the price to buy an area is proportional to its size, so he changes his mind. Now he wants to buy a part like this: If the part's size is A, and contains B gold mines, then A/B will be minimum among all the possible parts he can choose. Now, please tell him that minimum number, if all the parts he can choose has B=0, just output -1.
For each test case, the first line is two integers N(3<=N<=200) and M(1<=M<=500), the number of vertexs and the number of mines. Then N lines follows, the i-th line contains two integers xi,yi(-5000<=xi,yi<=5000), describing the position of the i-th vertex you can choose. Then M lines follow, the i-th line contains two integers xi,yi(-5000<=xi,yi<=5000), describing the position of the i-th mine.
For the second case, we can choose a polygon ( (0,0),(0,5),(2,2),(5,0) ) with A=10 and B=2, if we choose a
triangle ( (0,0),(0,5),(5,0) ), then A=12.5 and B=2.
For the third case, whatever we choose, we can't have a polygon contain the mines.
题目大意:给一个n可选取的顶点,m个金矿的坐标,求选取一个多边形它的面积与它所含金矿的个数的比值最少,找不到一个含有金矿的多边形的情况输出-1。
解题思路:可以证明一个最小比值的三角形(a/b)与其它三角形合起来形成的多边形肯定要大于a/b(a/b<min{a1/b1,a2/b2,...an/bn}时,a/b<(a+a1+a2..an)/(b+b1+b2+..+bn))。预处理每条直线上方金矿数量,那么三角形所含金矿数就等于abs(num[i][k]-num[i][j]-num[j][k])(画下图就知道了)。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int maxn=;
const double eps=1e-;
const double inf=1000000000.0;
int n,m,num[maxn][maxn];
struct Point
{
int x,y;
Point(int x=,int y=):x(x),y(y) {}
}a[maxn],b[maxn];
typedef Point Vector;
Vector operator -(Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
int Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;}//叉积
inline double min(double a,double b){return a<b?a:b;}
bool compx(Point a,Point b){return a.x<b.x;} void init()
{
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
int temp=;
for(int k=;k<m;k++)
if(a[i].x<=b[k].x && b[k].x<a[j].x && Cross(a[j]-a[i],b[k]-a[i])>)
temp++;
num[i][j]=temp;
}
}
} int main()
{
int i,j,k,t,icase=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++) scanf("%d%d",&a[i].x,&a[i].y);
sort(a,a+n,compx);
for(i=;i<m;i++) scanf("%d%d",&b[i].x,&b[i].y);
init();
double ans=inf;
for(i=;i<n;i++)
{
for(j=i+;j<n;j++)
{
for(k=j+;k<n;k++)
{
int temp=abs(num[i][k]-num[i][j]-num[j][k]);
if(temp==) continue;
ans=min(ans,fabs(Cross(a[j]-a[i],a[k]-a[i])/2.0)/temp);
}
}
}
if(fabs(ans-inf)<eps) printf("Case #%d: -1\n",++icase);
else printf("Case #%d: %.6lf\n",++icase,ans);
}
return ;
}
hdu 4353 统计点在三角形内的个数的更多相关文章
- 2D空间中判断一点是否在三角形内
要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y. 实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内. 效果: 代码 ...
- ACM: HDU 2563 统计问题-DFS+打表
HDU 2563 统计问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u HDU 2 ...
- hdu 4630 查询[L,R]区间内任意两个数的最大公约数
No Pain No Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 统计无向图中三角形的个数,复杂度m*sqrt(m).
统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #inclu ...
- hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)
围困 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 360(138 users) Total Accepted: 157(12 ...
- HDU 4353
利用分式的性质可以很容易证明要求的是个三角形,这很简单.对于求三角形内的雷的个数,只需求出每条边上方有多少个雷,作一点运算即可.如 A,B,C(B是X轴坐标在中间的点),则AC(其上方的雷的个数)-A ...
- 【Leetcode】判断平面中1个点是否落在三角形内
参考资料: 题目: https://blog.csdn.net/dongtinghong/article/details/78657403 符号重载: https://blog.csdn.net/cd ...
- UVA - 143 Orchard Trees (点在三角形内)
题意: 给出三角形的三个点的坐标(浮点数), 问落在三角形内及三角形边上的整点有多少? 思路:所有点暴力判断(点的范围1-99,三角形可能是0-100,因为这个WA了一下orz) AC代码: ...
随机推荐
- python 产生随机数
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- java基础—static关键字
一.static关键字
- C++实现Singleton模式(effective c++ 04)
阅读effective c++ 04 (31页) 提到的singleton设计模式.了解一下. 定义: 保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享. 应用场景: 比 ...
- Golang 简单 http 代理转发
程序基本实现了对http的完整转发,目前暂不支持https windows需要在设置中的网络>代理设置为手动,并开启代理服务器,填写ip和端口 // httpForward package ma ...
- LeetCode935
问题:935. 骑士拨号器 国际象棋中的骑士可以按下图所示进行移动: . 这一次,我们将 “骑士” 放在电话拨号盘的任意数字键(如上图所示)上,接下来,骑士将会跳 N-1 步. ...
- linux中怎样关闭ICMP回应功能
引用自:http://blog.csdn.net/qq844352155/article/details/49700121 linux中怎样关闭ICMP回应功能 输入: echo 1 > ...
- Spring Cloud构建微服务架构(二)分布式配置中心
注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...
- python网络-Socket之TCP编程(26)
一.TCP简介 1.TCP介绍 TCP协议,传输控制协议(英语:Transmission Control Protocol,缩写为 TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议. TC ...
- python 面向对象基础和高级复习
面向对象基础 面向对象编程 面向过程编程:类似于工厂的流水线 优点:逻辑清晰 缺点:扩展性差 面向对象编程:核心是对象二字,对象属性和方法的集合体,面向对象编程就是一堆对象交互 优点:扩展性强 缺点: ...
- SQL语句小练习
一.创建如下表结构(t_book) Id 主键 自增一 bookName 可变长 20 Price 小数 Author 可变长20 bookTypeId 图书类 ...