uva11168
uva11168
题意
给出一些点坐标,选定一条直线,所有点在直线一侧(或直线上),使得所有点到直线的距离平均值最小。
分析
显然直线一定会经过某两点(或一点),又要求点在直线某一侧,可以直接求出凸包,枚举每条边作为直线。
现在就要快速求出所有点到直线的距离,有求点到直线距离方程 \(\frac{|Ax_0 + By_0 + C|}{\sqrt{A^2+B^2}}\),注意所有点都在直线同一侧,所有 \(Ax_0 + By_0 + C\) 正负号相同,预处理出所有点横、纵坐标之和即可。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double INF = 1e18;
const int MAXN = 2e4 + 10;
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
bool operator < (const Point& p1) const {
if(x == p1.x) return y < p1.y;
return x < p1.x;
}
void read_point() {
scanf("%lf%lf", &x, &y);
}
};
double Cross(Point p1, Point p2) {
return p1.x * p2.y - p1.y * p2.x;
}
Point operator - (Point p1, Point p2) {
return Point(p1.x - p2.x, p1.y - p2.y);
}
int ConvexHull(Point* p, int n, Point* ch) {
sort(p, p + n);
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;
}
// (y - y1) / (y2 - y1) = (x - x1) / (x2 - x1)
// 得到直线 p1-p2 : A * x + B * y + C = 0
// 设 f(x, y) = A * x + B * y + C
// 若 f(x, y) < 0 表示点 (x, y) 在直线的左边(此时可把 p1-p2 当作向量)
void getLine(Point p1, Point p2, double& A, double& B, double& C) {
A = p2.y - p1.y; B = p1.x - p2.x; C = Cross(p2, p1);
}
Point p[MAXN], ch[MAXN];
int main() {
int kase = 1, T;
scanf("%d", &T);
while(T--) {
int n;
double X = 0, Y = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
p[i].read_point();
X += p[i].x;
Y += p[i].y;
}
int m = ConvexHull(p, n, ch);
double ans = INF;
for(int i = 0; i < m; i++) {
double A, B, C;
getLine(ch[i], ch[(i + 1) % m], A, B, C);
ans = min(ans, fabs(A * X + B * Y + C * n) / hypot(A, B) / n);
}
if(ans == INF) ans = 0;
printf("Case #%d: %.3f\n", kase++, ans);
}
return 0;
}
uva11168的更多相关文章
- UVA11168 Airport
题意 PDF 分析 首先发现距离最短的直线肯定在凸包上面. 然后考虑直线一般方程\(Ax+By+C=0\),点\((x_0,y_0)\)到该直线的距离为 \[ \frac{|Ax_0+By_0+C|} ...
- UVA 11168 - Airport - [凸包基础题]
题目链接:https://cn.vjudge.net/problem/UVA-11168 题意: 给出平面上的n个点,求一条直线,使得所有的点在该直线的同一侧(可以在该直线上),并且所有点到该直线的距 ...
随机推荐
- Binding and styling text to a RichTextBox in WPF
http://www.codeproject.com/Articles/137209/Binding-and-styling-text-to-a-RichTextBox-in-WPF The Rich ...
- Flex UI刷新后保持DataGrid中的ScrollBar的位置不变
这是之前我发的一个贴子问题描述:http://q.cnblogs.com/q/53469/
- win 7 64 位系统驱动签名
自己开发未经签名的驱动无法加载,关闭Windows 7系统中的驱动签名强制要求 bcdedit.exe -set loadoptions DDISABLE_INTEGRITY_CHECKS
- noip车站分级 拓扑排序
题目传送门 这道题呢 每次输入一段数就把1~n里面没有在这组数里面的数和他们连一波 表示这些数比他们等级低 然后就搞一搞就好了哇 #include<cstdio> #include< ...
- bzoj 1834
网络流的模板题 首先第一问我们直接用dinic搞就行了,费用直接存为0(时间上界非常松,这道题是能过),然后第二问我们只需要在第一问 的残余网络上加一个源点,源点指向1号点,容量为k,费用为0,然后对 ...
- hdu 2544 最短路 (dijkstra,floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找到两点间最短的距离值. 代码一:(dijkstra算法) #include < ...
- docker push 镜像到本地仓库
root@ubuntu:# uname -a Linux ubuntu --generic #-Ubuntu SMP Mon Feb :: UTC x86_64 x86_64 x86_64 GNU/L ...
- C++高精度
整理了一下高精度,虽然可用java,但很多时候还是C++写的方便. 附上kuangbin神的高精度模板(HDU1134 求卡特兰数) #include <iostream> #includ ...
- 安全测试===Mysql 注入技巧学习 MySQL注入技巧(1)
默认存在的数据库: mysql 需要root权限读取 information_schema 在5以上的版本中存在 测试是否存在注入方法 假:表示查询是错误的 (MySQL 报错/返回页面与原来不同) ...
- git放弃本地commit,未push
如果不小心commit了一个不应该commit的修改,但是还没有push,想撤销那个commit. 步骤: a) git log:获取commit id: b) git reset --hard co ...