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 ...
随机推荐
- 文件中的类都不能进行设计,因此未能为该文件显示设计器。设计器检查出文件中有以下类: FormMain --- 未能加载基类“WinForm.Win.FormsBase.FormMainBase”。请确保已引用该程序集并已生成所有项目
出现该问题的原因:FormMain从FormMainBase继承之后,一旦修改FormMainBase就会出现这个问题 解决方案:(1-4是搜索网友的) 1: 关闭VS所有窗口,后重启.即可返回正常. ...
- VS2010常用插件介绍
今天在写JS时,写到500多行时,感觉代码已经很难看了.想到C#代码都有折叠功能,是不是JS也有呢.在选项中找了一下,没有相关了的设置功能,于是就上网找.一找可就不得了,发现了好多好用的插件.都可以在 ...
- IntelliJ IDEA 15 安装
前言 因为今日14安装一插件崩溃,现安装了15. 版本:ideaIU-15.0.2 安装参考说明书 http://wiki.jikexueyuan.com/project/intellij-idea- ...
- Java---算法---插入排序
/** * 插入排序(升序) * * @param array */ public static void insertSort(int[] array) { int j = 0; // 下标从1开始 ...
- 【POJ】【2068】Nim
博弈论/DP 这是Nim?这不是巴什博奕的变形吗…… 我也不会捉啊,不过一看最多只有20个人,每人最多拿16个石子,总共只有8196-1个石子,范围好像挺小的,嗯目测暴力可做. so,记忆化搜索直接水 ...
- SSL 握手过程
SSL协议的握手过程 SSL 协议既用到了公钥加密技术又用到了对称加密技术,对称加密技术虽然比公钥加密技术的速度快,可是公钥加密技术提供了更好的身份认证技术.SSL 的握手协议非常有效的让客户和服务器 ...
- libvirt编译报错
virsh # list --all错误:连接到管理程序失败错误:无效的连接错误:将插槽连接到 '/usr/local/var/run/libvirt/libvirt-sock' 失败: 没有那个文件 ...
- Sublime Text 2 快捷键 插件配置
一.前言之前 从设计到前端,有过一段时间是懵懵懂懂的状态,缺乏对整个职业更加深入的了解.后来混迹于各个前端大牛的博客,在各个QQ群里聆听各路大神的经验之谈,坚定了前端之路的信心.一直收藏各类精华的帖子 ...
- SDUT1479数据结构实验之栈:行编辑器
先是普通的数组做法 #include<stdio.h> #include<string.h> int main() { ] ; while(~scanf("%s&qu ...
- Android ActionBar与ViewPager合用
内容可以左右滑动,因为使用了ViewPager(我这里用了V4包) view_pager_main.xml <?xml version="1.0" encoding=&quo ...