codeforces 394E Lightbulb for Minister 简单几何
题目链接:点我点我
题意:给定n个点。
以下n行给出这n个点坐标。
给定m个点,以下m行给出这m个点坐标。
这m个点是一个凸包,顺时针给出的。
问:在凸包上随意找一个点(x, y) 使得这个点距离n个点的距离平方和最小。
问这个最小的距离平方和是多少。
思路:
首先化简一下公式。把变量(x,y)提出来会发现是一个简单的函数,且开口向上。所以有唯一解,解出这个(x,y) 记为 (good_x, good_y)
但这个点可能不是坐落在凸包内,若坐落在凸包外。则最优解一定是在凸包的边上,所以枚举每条边求个解就好了。
推断点在凸多边形内部用三角形面积相等就可以。
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if (x>9) pt(x / 10);
putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5+10;
const int inf = 1e9;
const double eps = 1e-4;
struct Point{
double x, y;
Point(double a = 0, double b = 0) :x(a), y(b){}
}a[N], b[N];
int n, m;
double cx, cy, C;
double good_x, good_y;
double cal(double x, double y){
double ans = 0;
for (int i = 0; i < n; i++)
ans += (a[i].x - x)*(a[i].x - x) + (a[i].y - y)*(a[i].y - y);
return ans;
}
double area(Point x, Point y, Point z){
return abs(x.x*y.y + y.x*z.y + z.x*x.y - x.x*z.y - y.x*x.y - z.x*y.y) / 2.0;
}
double work(Point x){
double ans = 0;
for (int i = 0; i < m; i++)
ans += area(x, b[i], b[(i + 1) % m]);
return ans;
}
double papa(Point x){
return n*x.x*x.x + n*x.y*x.y - 2 * x.x*cx - 2 * x.y*cy;
}
Point cut(Point x, Point y, double k){
return Point(x.x + k*(y.x - x.x), x.y + k*(y.y - x.y));
}
double hehe(Point x, Point y){
double ans = min(papa(x), papa(y));
if (y.x != x.x){
double k = (y.y - x.y) / (y.x - x.x), b = x.y - k*x.x;
double _x = (k*cy + cx - n*k*b) / n / (1 + k*k);
if (_x < min(x.x, y.x) || _x > max(x.x, y.x))return ans;
double _y = k*_x + b;
ans = min(ans, papa(Point(_x, _y)));
}
else {
if (min(x.y, y.y) <= good_y && good_y <= max(x.y, y.y))
ans = min(ans, papa(Point(x.x, good_y)));
}
return ans;
}
int main(){
rd(n);
cx = cy = C = 0;
for (int i = 0; i < n; i++){
rd(a[i].x), rd(a[i].y);
cx += a[i].x;
cy += a[i].y;
C += a[i].x*a[i].x + a[i].y*a[i].y;
}
rd(m);
for (int i = 0; i < m; i++)rd(b[i].x), rd(b[i].y);
good_x = (double)cx / n;
good_y = (double)cy / n;
if (abs(work(b[0]) - work(Point(good_x, good_y))) < eps)
printf("%.10f\n", cal(good_x, good_y));
else {
double ans = 1e19;
for (int i = 0; i < m; i++)
ans = min(ans, hehe(b[i], b[(1 + i) % m]));
printf("%.10f\n", ans + C);
}
return 0;
}
codeforces 394E Lightbulb for Minister 简单几何的更多相关文章
- CodeForces 703C Chris and Road (简单几何)
题意:有一个n边形的汽车向以速度v向x轴负方向移动,给出零时时其n个点的坐标.并且有一个人在(0,0)点,可以以最大速度u通过w宽的马路,到达(0,w)点.现在要求人不能碰到汽车,人可以自己调节速度. ...
- Codeforces 935 简单几何求圆心 DP快速幂求与逆元
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- Codeforces 828B Black Square(简单题)
Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...
- Python下opencv使用笔记(二)(简单几何图像绘制)
简单几何图像一般包含点.直线.矩阵.圆.椭圆.多边形等等.首先认识一下opencv对像素点的定义. 图像的一个像素点有1或者3个值.对灰度图像有一个灰度值,对彩色图像有3个值组成一个像素值.他们表现出 ...
- Codeforces Round #231 (Div. 2) E.Lightbulb for Minister
题意:有n个点,问在一个m边形内哪个点与这n个点的距离平方和最小 题解:(ai-a0)^2=ai*ai+a0*a0-a*ai*a0 合起来就是a1*a1+...+an*an+n*a0*a0-2*a0* ...
- ACM: CodeForces 140A New Year Table-数学几何
CodeForces 140A New Year Table Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- CodeForces 459A Pashmak and Garden(水~几何-给两点求两点组成正方形)
题目链接:http://codeforces.com/problemset/problem/459/A 题目大意: 给出两个点(在坐标轴中),求另外两个点从而构成一个正方形,该正方形与坐标轴平行. 如 ...
- 简单几何(线段相交) POJ 2653 Pick-up sticks
题目传送门 题意:就是小时候玩的一种游戏,问有多少线段盖在最上面 分析:简单线段相交,队列维护当前最上的线段 /******************************************** ...
- codeforces 340C Tourist Problem(简单数学题)
题意:固定起点是0,给出一个序列表示n个点,所有点都在一条直线上,其中每个元素代表了从起点到这个点所走的距离.已知路过某个点不算到达这个点,则从起点出发,到达所有点的方案有许多种.求所有方案走的总路程 ...
随机推荐
- 695. Max Area of Island@python
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- 关于python字符串拼接的几种方法
当时看完python的基本语法后 给朋友写了个美元概率换算 写完后拼接结果时候 发现压根不知道python怎么拼接字符串 看了些资料自己做了个总结 首先就是和JavaScript一样的拼接方式 nam ...
- Gym-101615C-Fear Factoring(数论)
分析 题意是求 L - R之间的数的因数和 我们知道如果对于一个数 i ( i < k = sqrt(R)),那么一定有一个数 R/i 也是R的因数 遍历 i = 2 - k,然后对于每一个 i ...
- Tiny4412 U-BOOT移植(转)
http://blog.csdn.net/eshing/article/details/37520291(转) 一.移植前说明: 1. 特别声明:此文档是我的学习文档,里面肯定有错误地方,仅供参考! ...
- 07 mongodb
mongodb mongodb简介 简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为Web应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据 ...
- python013 Python3 循环语句
Python3 循环语句本章节将为大家介绍Python循环语句的使用.Python中的循环语句有 for 和 while.Python循环语句的控制结构图如下所示: while 循环Python中wh ...
- Codeforces Round #377 (Div. 2)部分题解A+B+C!
A. Buy a Shovel 题意是很好懂的,一件商品单价为k,但他身上只有10块的若干和一张r块的:求最少买几件使得不需要找零.只需枚举数量判断总价最后一位是否为0或r即可. #include&l ...
- 【Kubernetes】kube-dns 持续重启
kuberbetes部署和启动正常,但是kube-dns持续重启 使用命令 kubectl get pods --all-namespaces 得到结果 从图中可以看出kube-dns-c7d8589 ...
- linux rdesktop远程Win7老是提示密码错误问题解决
最近使用rdesktop远程Win7老是提示密码错误,输了N次,无比确认密码是正确的. 在Win7系统本身登录也是正常的. 但rdesktop远程就是报密码错误. 开始怀疑更新了最新版本问题,但是使用 ...
- 2016 Multi-University Training Contest 3 solutions BY 绍兴一中
1001 Sqrt Bo 由于有\(5\)次的这个限制,所以尝试寻找分界点. 很容易发现是\(2^{32}\),所以我们先比较输入的数字是否比这个大,然后再暴力开根. 复杂度是\(O(\log\log ...