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之生成器的初识
1. 生成器的定义 生成器的本质就是迭代器.python社区生成器和迭代器是一种 2. 生成器和迭代器区别 迭代器: 都是Python给你提供的已经写好的工具或者通过数据转化得来的 生成器: ...
- Logistic回归,梯度上升算法理论详解和实现
经过对Logistic回归理论的学习,推导出取对数后的似然函数为 现在我们的目的是求一个向量,使得最大.其中 对这个似然函数求偏导后得到 根据梯度上升算法有 进一步得到 我们可以初始化向量为0,或者随 ...
- <!DOCTYPE>声明
定义和用法 <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYPE> 声明不是 HTML 标签:它是指示 we ...
- 禅与 Objective-C 编程艺术(Zen and the Art of the Objective-C Craftsmanship)
英文版Zen and the Art of the Objective-C Craftsmanshiphttps://github.com/objc-zen/objc-zen-book 中文版禅与 O ...
- Clang提供的办法
1.方法弃用警告 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarati ...
- php 正则表达式中的 .*? 表示什么意思
我们知道我 .* 是任意字符,有的时候比较困惑在加个?什么意思. ?是非贪婪模式.*会匹配后面的一切字符,就是到结束的意思加?后就是不贪婪模式,这时要看?后边的字符是什么了,如.*?"的意思 ...
- HTML5一些特殊效果分享地址集合
页面预加载图片原生js: http://www.cnblogs.com/st-leslie/articles/5274568.html HTML5 FileReader读取本地文件: http://n ...
- DeepFaceLab小白入门(3):软件使用!
换脸程序执行步骤,大部分程序都是类似.DeepFaceLab 虽然没有可视化界面,但是将整个过程分成了8个步骤,每个步骤只需点击BAT文件即可执行.只要看着序号,一个个点过去就可以了,这样的操作应该不 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- STM32F407VET6之IAR之ewarm7.80.4工程建立(基于官方固件库1.6版本) 的工程文件目录
最后整理结构如下所示,├─cmsis│ startup_stm32f401xx.s│ startup_stm32f40xx.s│ startup_stm32f40_41xxx.s│ startup_s ...