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代码: ...
随机推荐
- 数据结构C语言实现系列——线性表(单向链表)
#include <stdio.h> #include <stdlib.h> #define NN 12 #define MM 20 typedef int elemType ...
- iOS应用架构谈part4-本地持久化方案及动态部署
前言 嗯,你们要的大招.跟着这篇文章一起也发布了CTPersistance和CTJSBridge这两个库,希望大家在实际使用的时候如果遇到问题,就给我提issue或者PR或者评论区.每一个issue和 ...
- ubuntu18.04+win10解决时钟不同步办法
安装ntpdate: 执行命令: sudo apt-get install ntpdate 设置校正服务器: sudo ntpdate time.windows.com 设置硬件时间为本地时间: 执行 ...
- ThinkPHP5 高级查询之构建分组条件
ThinkPHP5 高级查询之构建分组条件 一.在tp5中通过where方法如何构建分组条件, 例如:where user_id=$this->user_id and (status in (4 ...
- QT入门学习笔记2:QT例程
转至:http://blog.51cto.com/9291927/2138876 Qt开发学习教程 一.Qt开发基础学习教程 本部分博客主要根据狄泰学院唐老师的<QT实验分析教程>创作,同 ...
- leetcode-15-basic-string
58. Length of Last Word 解题思路: 从结尾向前搜索,空格之前的就是最后一个词了.写的时候我考虑了尾部有空格的情况.需要注意的是,测试用例中有" "的情况,此 ...
- 思维水题:UVa512-Spreadsheet Tracking
Spreadsheet Tracking Data in spreadsheets are stored in cells, which are organized in rows (r) and c ...
- The 2018 ACM-ICPC Chinese Collegiate Programming Contest Maximum Element In A Stack
//利用二维数组模拟 #include <iostream> #include <cstdio> #include <cstring> #include <s ...
- nw335 debian sid x86-64 -- 1 需求介绍
自己的台式机上面有有线网卡,路由器在客厅,托一条长长的线,关门也不方便.没有选择PCI无线网卡,没有选择nano类型的迷你网卡.买了nw335,带一条5DB天线,信号应该会好点.于是,开始了在debi ...
- Linux学习-逻辑滚动条管理员 (Logical Volume Manager)
LVM 可以整合多个实体 partition 在一起, 让这些 partitions 看起来就像是一个磁盘一样!而且,还可以在未来新增或移除其他的实 体 partition 到这个 LVM 管理的磁盘 ...