要点

  • 找凸包上的线很显然
  • 但每条线所有点都求一遍显然不可行,优化方法是:所有点都在一侧所以可以使用直线一般式的距离公式\(\frac{|A* \sum{x}+B* \sum{y}+C*n|}{\sqrt {A^2+B^2}}\)\(O(1)\)算出总距离
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; typedef double db;
const int maxn = 1e4 + 5;
const db eps = 1e-8; int dcmp(db x) {
if (fabs(x) < eps) return 0;
return x > 0 ? 1 : -1;
} int T, n, cnt;
struct Point {
db x, y; Point(){} Point(db a, db b):x(a), y(b){} bool operator < (const Point &rhs) const {
if (dcmp(x - rhs.x) != 0) return dcmp(x - rhs.x) < 0;
return dcmp(y - rhs.y) < 0;
}
}p[maxn];
Point v[maxn]; db Cross(Point A, Point B) {//顺时针转动则叉积为负
return A.x * B.y - A.y * B.x;
} Point operator - (Point A, Point B) {
return Point(A.x - B.x, A.y - B.y);
} bool operator == (Point A, Point B) {
return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0;
} void ConvexHull(int n) {
cnt = 0;
sort(p, p + n);
n = unique(p, p + n) - p;//去重 for (int i = 0; i < n; i++) {
while (cnt > 1 && dcmp(Cross(v[cnt - 1] - v[cnt - 2], p[i] - v[cnt - 2])) <= 0) cnt--;
v[cnt++] = p[i];
}
int k = cnt;
for (int i = n - 2; ~i; --i) {
while (cnt > k && dcmp(Cross(v[cnt - 1] - v[cnt - 2], p[i] - v[cnt - 2])) <= 0) cnt--;
v[cnt++] = p[i];
}
if (n > 1) cnt--;
} db Solve() {
if (n == 1) return 0;//特判
db res = 1e18, X = 0, Y = 0; for (int i = 0; i < n; i++) {
X += p[i].x;
Y += p[i].y;
}
for (int i = 0; i < cnt; i++) {
Point a = v[i], b = v[(i + 1) % cnt];
db A = b.y - a.y, B = a.x - b.x, C = Cross(b, a);
db calc = fabs((A * X + B * Y + C * n) / sqrt(A * A + B * B));
if (dcmp(calc - res) < 0) {
res = calc;
}
} return res / n;
} int main() {
scanf("%d", &T);
for (int kase = 1; kase <= T; kase++) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y); ConvexHull(n);//求凸包
printf("Case #%d: %.3lf\n", kase, Solve());
}
}

UVa 11168(凸包、直线一般式)的更多相关文章

  1. UVa 11168 (凸包+点到直线距离) Airport

    题意: 平面上有n个点,求一条直线使得所有点都在直线的同一侧.并求这些点到直线的距离之和的最小值. 分析: 只要直线不穿过凸包,就满足第一个条件.要使距离和最小,那直线一定在凸包的边上.所以求出凸包以 ...

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

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

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

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

  4. uva 11168

    题意:给出平面上的n个点,求一条直线,使得所有点在该直线的同一侧且所有点到该直线的距离和最小,输出该距离和. 思路:要使所有点在该直线的同一侧,明显是直接利用凸包的边更优.所以枚举凸包的没条边,然后求 ...

  5. UVA 11168 Airport(凸包)

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

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

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

  7. uva 11168 - Airport

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

  8. UVa 10256 凸包简单应用

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. CodeForces - 605C 凸包+直线与凸包判交

    题目大意: 要完成两种属性p,q的需求,给定n个双属性物品及其单位个物品中含有的属性,要求选择最少的物品来达成属性需求.(可以选择实数个物品) 题解: 实际上是一种属性混合问题 我们知道对于两种双属性 ...

随机推荐

  1. 搭建LoadRunner中的场景(三)场景的执行计划

    所谓场景操作,包括初始化用户组.启动用户组各用户以及停止虚拟用户的全过程.依据设置不同,执行过程中可以最多有5类操作,分别是启动用户组(start group).初始化(Initialize).启动虚 ...

  2. java 基础 - 查找某个字串出现的次数及位置

    查找某个字串出现的次数及位置 public class search { public static void main(String[] args){ String str = "abc1 ...

  3. elasticsearch ——id字段说明,内部是_uid

    _id field Each document indexed is associated with a _type (see the section called “Mapping Typesedi ...

  4. Android Studio的技巧

    1.快速添加add unimplements methods:  右键generate 2.快速添加try-catch:左边就有一个小电灯,然后可以选. 3.格式化OPTION + CMD + L ( ...

  5. zepto.fullpage

    内容来自:颜海镜 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. 多线程mtr-代码

    #!/bin/env python # -*- coding: utf-8 -*- # @Date : 2015-09-06 11:30:48 # @Author : Your Name (you@e ...

  7. 【Lintcode】087.Remove Node in Binary Search Tree

    题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given v ...

  8. POJ2182(排队问题)

    Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10695   Accepted: 6865 Descri ...

  9. sulime的必备插件

    常用插件 : SideBarEnhancements HTML-CSS-JS Prettify BracketHighlighter SublimeCodeIntel Emmet CTags Mark ...

  10. cassandra自启动服务器脚本

    新建一个文件. vi /etc/rc.d/init.d/vi cassandra2 #!/bin/sh # chkconfig: # description: service -- # process ...