题目传送门

题意:找一条直线,使得其余的点都在直线的同一侧,而且使得到直线的平均距离最短。

分析:训练指南P274,先求凸包,如果每条边都算一边的话,是O (n ^ 2),然而根据公式知直线一般式为Ax + By + C = 0.点(x0, y0)到直线的距离为:fabs(Ax0+By0+C)/sqrt(A*A+B*B)。

所以只要先求出x的和以及y的和,能在O (1)计算所有距离和。

两点式直线方程p1 (x1, y1),p2 (x2, y2)转换成一般式直线方程:A = y1 - y2, B = x2 - x1, C = -A * x1 - B * y1;

/************************************************
* Author :Running_Time
* Created Time :2015/11/10 星期二 11:24:09
* File Name :UVA_11168.cpp
************************************************/ #include <bits/stdc++.h>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int dcmp(double x) {
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
struct Point {
double x, y;
Point () {}
Point (double x, double y) : x (x), y (y) {}
Point operator - (const Point &r) const { //向量减法
return Point (x - r.x, y - r.y);
}
Point operator * (double p) const { //向量乘以标量
return Point (x * p, y * p);
}
Point operator / (double p) const { //向量除以标量
return Point (x / p, y / p);
}
Point operator + (const Point &r) const {
return Point (x + r.x, y + r.y);
}
bool operator < (const Point &r) const {
return x < r.x || (x == r.x && y < r.y);
}
bool operator == (const Point &r) const {
return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0;
}
};
typedef Point Vector;
Point read_point(void) {
double x, y; scanf ("%lf%lf", &x, &y);
return Point (x, y);
}
double dot(Vector A, Vector 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;
}
double length(Vector V) {
return sqrt (dot (V, V));
}
vector<Point> convex_hull(vector<Point> ps) {
sort (ps.begin (), ps.end ());
ps.erase (unique (ps.begin (), ps.end ()), ps.end ());
int n = ps.size (), k = 0;
vector<Point> qs (n * 2);
for (int i=0; i<n; ++i) {
while (k > 1 && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-2]) <= 0) k--;
qs[k++] = ps[i];
}
for (int t=k, i=n-2; i>=0; --i) {
while (k > t && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-2]) <= 0) k--;
qs[k++] = ps[i];
}
qs.resize (k-1);
return qs;
} double sqr(double x) {
return x * x;
} int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
int n; scanf ("%d", &n);
vector<Point> ps;
double x, y, sx = 0, sy = 0;
for (int i=0; i<n; ++i) {
scanf ("%lf%lf", &x, &y);
sx += x; sy += y;
ps.push_back (Point (x, y));
}
vector<Point> qs = convex_hull (ps);
if ((int) qs.size () <= 2) {
printf ("Case #%d: %.3f\n", ++cas, 0.0); continue;
}
double mn = 1e9;
qs.push_back (qs[0]);
for (int i=0; i<qs.size ()-1; ++i) {
double A = qs[i].y - qs[i+1].y;
double B = qs[i+1].x - qs[i].x;
double C = -A * qs[i].x - B * qs[i].y;
double tmp = fabs (A * sx + B * sy + n * C) / sqrt (sqr (A) + sqr (B));
if (mn > tmp) mn = tmp;
}
printf ("Case #%d: %.3f\n", ++cas, mn / n);
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}

  

简单几何(数学公式+凸包) UVA 11168 Airport的更多相关文章

  1. UVA 11168 Airport(凸包)

    Airport [题目链接]Airport [题目类型]凸包 &题解: 蓝书274页,要想到解析几何来降低复杂度,还用到点到直线的距离公式,之后向想到预处理x,y坐标之和,就可以O(1)查到距 ...

  2. UVA 11168 Airport(凸包+直线方程)

    题意:给你n[1,10000]个点,求出一条直线,让所有的点都在都在直线的一侧并且到直线的距离总和最小,输出最小平均值(最小值除以点数) 题解:根据题意可以知道任意角度画一条直线(所有点都在一边),然 ...

  3. UVA 11168 - Airport - [凸包基础题]

    题目链接:https://cn.vjudge.net/problem/UVA-11168 题意: 给出平面上的n个点,求一条直线,使得所有的点在该直线的同一侧(可以在该直线上),并且所有点到该直线的距 ...

  4. 简单几何(求凸包点数) POJ 1228 Grandpa's Estate

    题目传送门 题意:判断一些点的凸包能否唯一确定 分析:如果凸包边上没有其他点,那么边想象成橡皮筋,可以往外拖动,这不是唯一确定的.还有求凸包的点数<=2的情况一定不能确定. /********* ...

  5. 简单几何(四边形形状) UVA 11800 Determine the Shape

    题目传送门 题意:给了四个点,判断能构成什么图形,有优先规则 分析:正方形和矩形按照点积为0和长度判断,菱形和平行四边形按向量相等和长度判断,梯形按照叉积为0判平行.因为四个点是任意给出的,首先要进行 ...

  6. uva 11168 - Airport

    凸包+一点直线的知识: #include <cstdio> #include <cmath> #include <cstring> #include <alg ...

  7. 简单几何(推公式) UVA 11646 Athletics Track

    题目传送门 题意:给了长宽比例,操场一圈400米,问原来长宽的长度 分析:推出公式 /************************************************ * Author ...

  8. 简单几何(求交点) UVA 11437 Triangle Fun

    题目传送门 题意:三角形三等分点连线组成的三角形面积 分析:入门题,先求三等分点,再求交点,最后求面积.还可以用梅涅劳斯定理来做 /********************************** ...

  9. 简单几何(求交点) UVA 11178 Morley's Theorem

    题目传送门 题意:莫雷定理,求三个点的坐标 分析:训练指南P259,用到了求角度,向量旋转,求射线交点 /*********************************************** ...

随机推荐

  1. JavaScript中,window.opener是什么?window.parent和window.opener有啥区别?

    来自CSDN的问答: window.opener是什么啊? ++++++++++++++++++++++++++++++++++++++++++++++++++ 弹出本窗体的句柄 比如你想点一个按钮直 ...

  2. TCPIP三次握手详情

    TCP正常建立和关闭的状态变化 TCP连接的建立可以简单的称为三次握手,而连接的中止则可以叫做 四次握手. 建立连接 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第 ...

  3. [Effective JavaScript 笔记]第25条:使用bind方法提取具有确定接收者的方法

    js里方法和属性值为函数,就像一个东西两种称呼一个样,比如土豆,也叫马铃薯,一个样.既然一样,那就可以对对象的方法提取出来为函数,然后把提取出来的函数作为回调函数直接传递给高阶函数. 高阶函数是什么 ...

  4. unity3d 参考坐标系

    原地址:http://www.cnblogs.com/88999660/archive/2013/04/01/2993844.html 参考坐标系(Reference Coordinate Syste ...

  5. unity3d webplayer 16:9 居中显示模板

    原地址:http://www.cnblogs.com/88999660/archive/2013/04/12/3016773.html <!DOCTYPE html PUBLIC "- ...

  6. 使用Cydia Substrate 从Native Hook Android Java世界

    这里介绍了如何使用Cydia Substrate Hook安卓Java世界.这篇文章介绍如何从Native中Hook 安卓Java世界. 手机端配置见之前文章. 一.建立工程 建立一个Android工 ...

  7. PHP 的__call()

    PHP5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它方法.如果你试着调用一个对象中不存在或被权限控制中的方法,__call 方法将会被自动调用. 例七:__call ...

  8. jdk新特性

    自动拆装箱子: import org.junit.Test; public class Demo { /* * 自动拆装箱 * */ @Test public void ZhuangXiang() { ...

  9. iOS 中使用类别简化代码开发

    最近需要一个函数,把CLLocation对象转化为NSDictionary,按照我以前的想法,我会写一个工具类,之后添加一个函数,类似这样 - (NSDictionary *)turnLocation ...

  10. LLVM,Clang

    在使用xcode时常常会遇到这2个概念,今天总结一下. wiki中关于llvm的描述: LLVM提供了完整編譯系統的中間層,它會將中間語言(IF, Intermediate form)從編譯器取出與最 ...