点击打开链接

Problem D
Airport
Input:
Standard Input

Output: Standard Output

It is no coincidence  that in no known language
  does the phrase 'As pretty as an Airport' appear.

Douglas Adams

There is a small town with n houses. The town needs an airport. An airport is basically a very long, very straight road. Think of it as an infinite line. We need to build the airport such that the average distance from each house to the airport is as small as possible. However, no one wants to walk across the runway, so all of the houses must be on the same side of the airport. (Some houses may be a distance of zero away from the runway, but that's ok; we'll give them some free ear plugs.)

Where should we build the airport, and what will be the average distance?

Input
The first line of input gives the number of cases, N (<=65) .N test cases follow. Each one is a line containing n (0<  n <= 10000) , followed by n lines giving the xy-coordinates of the houses. All coordinates are integers with absolute value of at most 80,000 .

Output

For each test case, output one line containing "Case #x:" followed by the average distance from the airport to the houses, with 3 digits after the decimal point. No answer will be within 10-5 of a round-off error case.

Sample Input                             Output for Sample Input

4
4
0 0
0 1
1 0
1 1
2
15035 39572
34582 39535
3
0 0
0 1
1 0
5
0 0
0 2
2 0
2 2
1 1
Case #1: 0.500
Case #2: 0.000
Case #3: 0.236
Case #4: 1.000

Problemsetters: Igor Naverniouk and Derek Kisman

题意:给出平面上n个点,找一条直线,使得所有的点在直线的同侧(也可以在直线上),且到直线的距离之和尽量小。

A了一天,WA了十多次,终于过了。不容易啊。要选择这么一条直线,不难发现,选择凸包的边所在的直线是最优。由于凸包上的边不超过n条,则只需O(n)时间就会解决这个问题。设直线的一般式方程为Ax+By+C=0,则点(x0,y0)到直线的距离是为:|Ax0+By0+C|/sqrt(A^2+B^2)。因为所有的点在同一侧,所有的点的正负号相同。这样我们先处理所有点的x坐标和y坐标之和。首先要注意定义无穷大的时候不能定义成999999,如果这样会是WA。而要定义成0x3f3f3f3f。然后要注意unique的使用,要去重。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double inf=0x3f3f3f3f;
int n;
double sumx,sumy;
const double eps = 1e-10;
struct Point
{double x,y;Point(double x=0,double y=0):x(x),y(y){}//构造函数bool operator < (const Point& a) const{if(a.x != x) return x < a.x;return y < a.y;}
};
typedef Point Vector;
Point P[10010],ch[10010];//向量+向量=向量,点+向量=点
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);
}
int dcmp(double x)
{if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}
bool operator == (const Point& a, const Point &b)
{return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
//就算OA和OB的叉积
double Cross(Point A,Point B)
{return A.x*B.y-A.y*B.x;
}//凸包
int ConvexHull(Point *p,int n,Point *ch)//ch是空的
{sort(p,p+n);//x按照从小到大排序,若x相等,按照y从小到大排序n=unique(p,p+n)-p;int m=0;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--;return m;//返回的是凸包的顶点数,ch数组存的是凸包的顶点
}//总的点到Ax+By+C的距离
double get(double A,double B,double C)
{double k=fabs(A*sumx+B*sumy+n*C);double v=sqrt(A*A+B*B);return k/v;
}
//Ax+By+C=0
double getDist(Point a,Point b)
{double A=a.y-b.y;double B=b.x-a.x;double C=a.x*b.y-a.y*b.x;return get(A,B,C);
}int main()
{int t,cas=1;scanf("%d",&t);while(t--){sumx=0,sumy=0;double temp,minn=inf;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%lf%lf",&P[i].x,&P[i].y);sumx+=P[i].x;sumy+=P[i].y;}printf("Case #%d: ",cas++);if(n<=2){printf("0.000\n");continue;}int m=ConvexHull(P,n,ch);for(int i=0;i<m;i++){//printf("%lf %lf      %lf %lf\n",ch[i].x,ch[i].y,ch[i+1].x,ch[i+1].y);temp=getDist(ch[i],ch[(i+1)%m]);//printf("其余点点到i和i+1的距离是:%lf  %lf\n",temp,temp/(nn*1.0));minn=min(minn,temp);}double ans=minn/n;printf("%.3lf\n",ans);}return 0;
}

UVA 11168 - Airport 凸包相关推荐

  1. UVa 11168 Airport , 凸包

    题意: 给出平面上n个点,找一条直线,使得全部点在直线的同側.且到直线的距离之平均值尽量小. 先求凸包 易知最优直线一定是凸包的某条边,然后利用点到直线距离公式进行计算. #include<cs ...

  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. 【Smart_Point】C/C++ 中智能指针
  2. 记一次数据中心云平台系统项目实施
  3. opencv-python实现马赛克油画漫画风格的图片
  4. python元编程_Python 元编程
  5. 用jedis访问Redis进行对象存取示例
  6. 《Redis核心技术与实战》学习总结(2)
  7. java jqgrid json格式_jqGrid 数据之 Json
  8. MOVE降低高水位 HWM
  9. lan pci 联想开机_我的联想电脑开机老显示DHCP
  10. APK的几种安装方式
  11. 投影技术的分类与应用
  12. 影子传文件到服务器,影子传说——文件超级隐身术
  13. 基于51单片机的简易计算器proteus仿真 数码管显示
  14. 小林coding 的笔记——图解网络(一)
  15. 解决error C2059: 语法错误:“::”问题
  16. 百度地图API乡镇级别行政区划
  17. 数据压缩作业:AVI格式文件分析
  18. 【论文解读 ICLR 2020 | DropEdge】TOWARDS DEEP GRAPH CONVOLU-TIONAL NETWORKS ON NODE CLASSIFICATION
  19. jquery的在线api
  20. 【2021/推荐/社交网络】Socially-Aware Self-Supervised Tri-Training for Recommendation

热门文章

  1. python实现k均值聚类(kMeans)基于numpy
  2. 【愚公系列】2023年05月 网络安全高级班 035.HW护网行动攻防演练(0day漏洞防护)
  3. 七夕了没有男/女朋友怎么办?孤寡机器人帮你!
  4. 计算机网络——第四章
  5. 海外营销人员福音:新的TikTok工具,用过的都说好
  6. 基层员工和领导者的区别,悟到这一层,你也会腾飞!
  7. 【数值分析】学习笔记4——凸优化2:拉格朗日函数及变分不等式(Variational Inequality,VI)
  8. Problem H: 小学生算术
  9. 老徐WEB:最简单详细的轮播图原理和制作过程(一)
  10. 下面请您欣赏相声:文武双全,和反思