简单几何(数学公式+凸包) UVA 11168 Airport
题意:找一条直线,使得其余的点都在直线的同一侧,而且使得到直线的平均距离最短。
分析:训练指南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的更多相关文章
- UVA 11168 Airport(凸包)
Airport [题目链接]Airport [题目类型]凸包 &题解: 蓝书274页,要想到解析几何来降低复杂度,还用到点到直线的距离公式,之后向想到预处理x,y坐标之和,就可以O(1)查到距 ...
- UVA 11168 Airport(凸包+直线方程)
题意:给你n[1,10000]个点,求出一条直线,让所有的点都在都在直线的一侧并且到直线的距离总和最小,输出最小平均值(最小值除以点数) 题解:根据题意可以知道任意角度画一条直线(所有点都在一边),然 ...
- UVA 11168 - Airport - [凸包基础题]
题目链接:https://cn.vjudge.net/problem/UVA-11168 题意: 给出平面上的n个点,求一条直线,使得所有的点在该直线的同一侧(可以在该直线上),并且所有点到该直线的距 ...
- 简单几何(求凸包点数) POJ 1228 Grandpa's Estate
题目传送门 题意:判断一些点的凸包能否唯一确定 分析:如果凸包边上没有其他点,那么边想象成橡皮筋,可以往外拖动,这不是唯一确定的.还有求凸包的点数<=2的情况一定不能确定. /********* ...
- 简单几何(四边形形状) UVA 11800 Determine the Shape
题目传送门 题意:给了四个点,判断能构成什么图形,有优先规则 分析:正方形和矩形按照点积为0和长度判断,菱形和平行四边形按向量相等和长度判断,梯形按照叉积为0判平行.因为四个点是任意给出的,首先要进行 ...
- uva 11168 - Airport
凸包+一点直线的知识: #include <cstdio> #include <cmath> #include <cstring> #include <alg ...
- 简单几何(推公式) UVA 11646 Athletics Track
题目传送门 题意:给了长宽比例,操场一圈400米,问原来长宽的长度 分析:推出公式 /************************************************ * Author ...
- 简单几何(求交点) UVA 11437 Triangle Fun
题目传送门 题意:三角形三等分点连线组成的三角形面积 分析:入门题,先求三等分点,再求交点,最后求面积.还可以用梅涅劳斯定理来做 /********************************** ...
- 简单几何(求交点) UVA 11178 Morley's Theorem
题目传送门 题意:莫雷定理,求三个点的坐标 分析:训练指南P259,用到了求角度,向量旋转,求射线交点 /*********************************************** ...
随机推荐
- [BZOJ1529][POI2005]ska Piggy banks
[BZOJ1529][POI2005]ska Piggy banks 试题描述 Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放 ...
- Populating Next Right Pointers in Each Node
这题代码简单,不过不容易想到. void connect(TreeLinkNode *root) { if (root == nullptr ||root->left==nullptr)retu ...
- linux 回收站的添加
在~下 .bashrc或者.bash_profile加入 mkdir -p ~/.trash alias rm=trash trash() { mv $@ ...
- OpenStack 的Nova组件详解
Open Stack Compute Infrastructure (Nova) Nova是OpenStack云中的计算组织控制器.支持OpenStack云中实例(instances)生命周期的所有活 ...
- /lib /usr/lib /usr/local/lib区别
昨天问我/usr/lib 和/usr/local/lib 我仅记得一个是系统的,一个是用户的,于是今天查了查,有两篇文章介绍的不错,usr 很多人都认为是user缩写,其实不然,是unix syste ...
- 《ASP.NET1200例》<ItemTemplate>标签在html里面有什么具体的作用
严格的来说 <ItemTemplate> 在html中无意义,他只是针对诸如 Repeater.DataList.GridView中的一个模板 至于里面的含义,你可以这样想,既然Repea ...
- tcp ip detatils
tcp ip detatils 8.关于TCP协议,下面哪种说法是错误的()A.TCP关闭连接过程中,两端的socket都会经过TIME_WAIT状态B.对一个Established状态的TCP连接, ...
- 【转】利用mybatis-generator自动生成代码
本文转自:http://www.cnblogs.com/yjmyzz/p/4210554.html mybatis-generator有三种用法:命令行.eclipse插件.maven插件.个人觉得m ...
- iOS 利用self.navigationItem.backBarButtonItem修改后退按钮文字
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem; // Bar button item to use for the ba ...
- 2.前端笔记之css
title: 1.前端笔记之CSS date: 2016-04-05 23:05:51 tags: 前端 categories: w3c --- 作者:刘耀 **出处:http://www.liuya ...