UVa 11168 (凸包+点到直线距离) Airport
题意:
平面上有n个点,求一条直线使得所有点都在直线的同一侧。并求这些点到直线的距离之和的最小值。
分析:
只要直线不穿过凸包,就满足第一个条件。要使距离和最小,那直线一定在凸包的边上。所以求出凸包以后,枚举每个边求出所有点到直线的距离之和得到最小值。
点到直线距离公式为:
因为点都在直线同一侧,所以我们可以把加法“挪”到里面去,最后再求绝对值,所以可以预处理所有点的横坐标之和与纵坐标之和。当然常数C也要记得乘上n倍。
已知两点坐标求过该点直线的方程,这很好求不再赘述,考虑到直线没有斜率的情况,最终要把表达式中的分母乘过去。
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std; struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y) {}
};
typedef Point Vector;
Point operator + (Point A, Point B)
{
return Point(A.x+B.x, A.y+B.y);
}
Point operator - (Point A, Point B)
{
return Point(A.x-B.x, A.y-B.y);
}
bool operator < (const Point& A, const Point& B)
{
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A, const Point& B)
{
return A.x == B.x && A.y == B.y;
}
double Cross(Vector A, Vector B)
{
return A.x*B.y - A.y*B.x;
} vector<Point> ConvexHull(vector<Point> p) {
// 预处理,删除重复点
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end()); int n = p.size();
int m = ;
vector<Point> ch(n+);
for(int i = ; i < n; i++) {
while(m > && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-; i >= ; i--) {
while(m > k && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
//for(int i = 0; i < m; ++i) printf("%lf %lf\n", ch[i].x, ch[i].y);
ch.resize(m);
return ch;
} double sumx, sumy; double Dist(Point a, Point b, int m)
{
double A = a.y-b.y, B = b.x-a.x, C = a.x*b.y - b.x*a.y;
//printf("%lf %lf", fabs(A*sumx+B*sumy+C), sqrt(A*A+B*B));
return (fabs(A*sumx+B*sumy+C*m) / sqrt(A*A+B*B));
} int main(void)
{
#ifdef LOCAL
freopen("11168in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
for(int kase = ; kase <= T; ++kase)
{
int n;
vector<Point> p;
sumx = 0.0, sumy = 0.0;
scanf("%d", &n);
for(int i = ; i < n; ++i)
{
double x, y;
scanf("%lf%lf", &x, &y);
p.push_back(Point(x, y));
sumx += x; sumy += y;
}
vector<Point> ch = ConvexHull(p);
int m = ch.size();
//for(int i = 0; i < m; ++i) printf("%lf %lf\n", ch[i].x, ch[i].y);
if(m <= )
{
printf("Case #%d: 0.000\n", kase);
continue;
} double ans = 1e10;
for(int i = ; i < m; ++i)
ans = min(ans, Dist(ch[i], ch[(i+)%m], n));
printf("Case #%d: %.3lf\n", kase, ans/n);
}
}
代码君
UVa 11168 (凸包+点到直线距离) Airport的更多相关文章
- OpenCV计算点到直线的距离 数学法
我们在检测图像的边缘图时,有时需要检测出直线目标,hough变换检测出直线后怎么能更进一步的缩小区域呢?其中,可以根据距离来再做一判断,就涉及到了点与直线的距离问题. 点到直线距离代码如下: //== ...
- UVA 11168 Airport(凸包)
Airport [题目链接]Airport [题目类型]凸包 &题解: 蓝书274页,要想到解析几何来降低复杂度,还用到点到直线的距离公式,之后向想到预处理x,y坐标之和,就可以O(1)查到距 ...
- UVA 11168 - Airport - [凸包基础题]
题目链接:https://cn.vjudge.net/problem/UVA-11168 题意: 给出平面上的n个点,求一条直线,使得所有的点在该直线的同一侧(可以在该直线上),并且所有点到该直线的距 ...
- POJ1584 判断多边形是否为凸多边形,并判断点到直线的距离
求点到直线的距离: double dis(point p1,point p2){ if(fabs(p1.x-p2.x)<exp)//相等的 { return fabs(p2.x-pe ...
- ArcGIS 点到直线的距离
/****点到直线的距离*** * 过点(x1,y1)和点(x2,y2)的直线方程为:KX -Y + (x2y1 - x1y2)/(x2-x1) = 0 * 设直线斜率为K = (y2-y1)/(x2 ...
- uva 11168 - Airport
凸包+一点直线的知识: #include <cstdio> #include <cmath> #include <cstring> #include <alg ...
- uva 12304点与直线与圆之间的关系
Problem E 2D Geometry 110 in 1! This is a collection of 110 (in binary) 2D geometry problems. Circum ...
- POJ1912 A highway and the seven dwarfs (判断凸包与直线相交 logn)
POJ1912 给定n个点 和若干条直线,判断对于一条直线,是否存在两个点在直线的两侧. 显然原命题等价于 凸包与直线是否相交. O(n)的算法是显而易见的 但是直线数量太多 就会复杂到O(n^2)由 ...
- POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内
首先判断是不是凸多边形 然后判断圆是否在凸多边形内 不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板 POJ 1584 A Round Peg in a G ...
随机推荐
- 在一个Activity里面的TextView上面添加网页链接,启动后到另一个Activity里面!
可以添加很多的属性,样式或者是什么的,目前要完成的功能是 点击TextView里面的某个文字链接,进入另外一个Activity里面!例如你可以做微博里面的 @XXX: 点击后进入他的个人主页! 下面都 ...
- setjmp和longjmp的使用
问题描述: setjmp和longjmp的使用 问题解决: setjmp和longjmp是C语言独有的,只有将它们结合起来使用,才能达到程序控制流有效转移的目的,按照程序 ...
- Oracle 临时事务表 全局临时表_global temporary table
所有的操作都在一个事务里,事务提交后,此表清空,特别适合做插入删除频率特别高的临时表操作,比如插入完数据就开始查询,查询完就删掉等,用完就扔! 临时表分事务级临时表和会话级临时表. 事务级临时表只对当 ...
- FreePlan Windows下默认乱码解决方案
FreePlan 做为一个开源的跨平台的思维导图软件非常好用. 笔者最近在Windows下使用时发现,新建导图文件时默认总是乱码,每次新建元素都需要手动设置一下字体才行. 研究一下,估计是默认模板问题 ...
- Open multiple Eclipse workspaces on the Mac
This seems to be the supported native method in OS X: cd /Applications/eclipse/ open -n Eclipse.app ...
- hdu 1596 find the safest road(最短路,模版题)
题目 这是用Dijsktra做的,稍加改动就好,1000ms..好水.. #define _CRT_SECURE_NO_WARNINGS #include<string.h> #inclu ...
- POJ 2379 ACM Rank Table(排序)
题很水,数据注意一下四点即可: 1.有些team会在一道题AC了之后还提交,这个时候只需要算第一次ac的时间以及这之前的wa,之后的全部忽略.2.如果一道题没有ac,那么在计算时间时不应该加上它的wa ...
- 2013 Multi-University Training Contest 1 Partition
这题主要是推公式…… ;}
- Error: The VPN client agent was unable to create the interprocess communication depot.
当安装Cisco AnyConnect VPN Client出现The VPN client agent was unable to create the interprocess communica ...
- 李洪强iOS开发之OC语言description方法和sel
OC语言description方法和sel 一.description方法 Description方法包括类方法和对象方法.(NSObject类所包含) (一)基本知识 -description(对象 ...