题意:

给出平面上n个点,找一条直线,使得全部点在直线的同側。且到直线的距离之平均值尽量小。

先求凸包

易知最优直线一定是凸包的某条边,然后利用点到直线距离公式进行计算。

#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;struct Point {int x, y;Point(int x=0, int y=0):x(x),y(y) {}
};typedef Point Vector;Vector operator + (const Vector& a, const Vector& b) {return Vector(a.x+b.x, a.y+b.y);
}
Vector operator - (const Vector& a, const Vector& b) {return Vector(a.x-b.x, a.y-b.y);
}
Vector operator * (const Vector& a, double p) {return Vector(a.x*p, a.y*p);
}
Vector operator / (const Vector& a, double p) {return Vector(a.x/p, a.y/p);
}
bool operator < (const Point& p1, const Point& p2){return p1.x<p2.x ||(p1.x==p2.x&&p1.y<p2.y);
}bool operator == (const Point& p1, const Point& p2){return p1.x == p2.x && p1.y == p2.y;
}int Cross(const Vector& a, const 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 = 0;vector<Point> ch(n+1);for(int i=0; i<n; ++i) {while(m>1&&Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])<=0) m--;ch[m++] = p[i];}int k = m;for(int i=n-2; i>=0; --i) {while(m>k&&Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])<=0) m--;ch[m++] = p[i];}if(n>1) m--;ch.resize(m);return ch;
}// 过两点p1, p2的直线一般方程ax+by+c=0
// (x2-x1)(y-y1) = (y2-y1)(x-x1)
void getLineGeneralEquation(const Point& p1, const Point& p2, double& a, double& b, double &c) {a = p2.y-p1.y;b = p1.x-p2.x;c = -a*p1.x - b*p1.y;
}int main()
{int t, n, i, j;scanf("%d", &t);for(int cas=1; cas<=t; ++cas){scanf("%d", &n);int x, y;vector<Point> P;double sumx = 0, sumy = 0;for(i=0; i<n; ++i){scanf("%d%d", &x, &y);sumx += x;sumy += y;P.push_back(Point(x,y));}P = ConvexHull(P);int m = P.size();double ans = 1e9;if(m<=2) ans = 0;elsefor(i=0; i<m; ++i){j = (i+1)%m;double A, B, C;getLineGeneralEquation(P[i], P[j], A, B, C);double tmp = fabs(A*sumx + B*sumy + C*n) / sqrt(A*A+B*B);ans = min(ans, tmp);}printf("Case #%d: %.3f\n", cas, ans/n);}return 0;
}

UVa 11168 Airport , 凸包相关推荐

  1. UVA 11168 - Airport 凸包

    点击打开链接 Problem D Airport Input: Standard Input Output: Standard Output It is no coincidence  that in ...

  2. 简单几何(数学公式+凸包) UVA 11168 Airport

    题目传送门 题意:找一条直线,使得其余的点都在直线的同一侧,而且使得到直线的平均距离最短. 分析:训练指南P274,先求凸包,如果每条边都算一边的话,是O (n ^ 2),然而根据公式知直线一般式为A ...

  3. UVa 10652 (简单凸包) Board Wrapping

    题意: 有n块互不重叠的矩形木板,用尽量小的凸多边形将它们包起来,并输出并输出木板总面积占凸多边形面积的百分比. 分析: 几乎是凸包和多边形面积的裸题. 注意:最后输出的百分号前面有个空格,第一次交P ...

  4. UVa 11374 - Airport Express

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. Uva 11374 - Airport Express(枚举+最短路)

    题目链接 https://vjudge.net/problem/UVA-11374 [题意] 市民从市区去机场要走机场快线,机场快线分为经济线和商业线两种,你只有一张商业线车票,只能坐一站商业线,其它 ...

  6. 叉积求点到平面距离_OpenCV计算点到直线的距离 数学法

    我们在检测图像的边缘图时,有时需要检测出直线目标,hough变换检测出直线后怎么能更进一步的缩小区域呢?其中,可以根据距离来再做一判断,就涉及到了点与直线的距离问题. 点到直线距离代码如下: //== ...

  7. oneplus2系统_OnePlus正在启动一个与苹果竞争的生态系统

    oneplus2系统 调试器 (Debugger) Over the last few years, Apple has built a formidable, deeply integrated e ...

  8. UVa 109 - SCUD Busters(凸包计算)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  9. UVA 109 SCUD Busters【凸包模拟题】

    题目大意:世界由几个互不重叠领土但彼此敌对的国家组成,每个国家有一个发电站,负责给本国发电. 1,给出每个国家的建筑数(包括发电站和房子数),每个国家用最少的围墙将本国保护起来(凸包): 2,现在有不 ...

最新文章

  1. iOS开发-Certificates、Identifiers和Profiles详解
  2. python sklearn.datasets.fetch_mldata MNIST手写数字数据集无法获取, 报错 Function fetch_mldata is deprecated 的解决办法
  3. VTK修炼之道5_Procedural Source Object
  4. 满天星_Java实例_源码+图片素材
  5. Xcode7将无需开发者授权也能在真机上调试App
  6. Http协议:状态码
  7. 工作组访问不到别人的计算机,众果搜的博客
  8. 20165332第八周课下作业
  9. 本周任务asp.net 1.1老系统移植升级到asp.net 2.0,又是一个浩大的工程啊?
  10. php pack方法,php pack()函数详解与示例
  11. PAT 1045 快速排序(25)(STL-set+思路+测试点分析)
  12. 012 Ceph多区域网关
  13. 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options...
  14. 使用可道云kodbox在ubuntu云服务器上搭建自己的私有云盘(详细教程)
  15. just for save
  16. python自动化测试面试题None is ==详解
  17. 四平方和python_成都房价现在多少?和北京差距很大吗?Python爬取了四万套房源!...
  18. 《论文阅读》Multi-Task Learning of Generation and Classification for Emotion-Aware Dialogue Response Gener
  19. via自定义搜索引擎代码_Via浏览器自定义主页
  20. ubuntu18.04 安装Adobe Flash Player

热门文章

  1. 步步为营:Asp.Net 淘宝通用应用接口攻略
  2. Netty入门系列(1) --使用Netty搭建服务端和客户端
  3. 如何使CPU占用率为50%
  4. 后台返回的数据换行显示
  5. AutoHotkey 使用笔记
  6. HTML5之Viewport详解
  7. java判断一个类是否公共类
  8. python的设计模式之MVC模式
  9. 雇佣最优秀的开发者?培养可能是更好的选择
  10. 多集群应用如何帮助企业级Kubernetes获益