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*(a1+...+an)
取导数就是2*n*a0-2*a0*(a1+...+an)
可以知道在x y轴上各取n个点的平均值就是最小值
至于在不在m边形里,判断一下吧
不在里面就在线上,m条边求下导,求出最小值的位置
#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 ;
while (c != '-' && (c<'' || c>'')) c = getchar();
sgn = (c == '-') ? - : ;
ret = (c == '-') ? : (c - '');
while (c = getchar(), c >= ''&&c <= '') ret = ret * + (c - '');
ret *= sgn;
return ;
}
template <class T>
inline void pt(T x) {
if (x <) {
putchar('-');
x = -x;
}
if (x>) pt(x / );
putchar(x % + '');
}
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5+;
const int inf = 1e9;
const double eps = 1e-;
struct Point{
double x, y;
Point(double a = , double b = ) :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 = ;
for (int i = ; 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 = ;
for (int i = ; i < m; i++)
ans += area(x, b[i], b[(i + ) % m]);
return ans;
}
double papa(Point x){
return n*x.x*x.x + n*x.y*x.y - * x.x*cx - * 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 / ( + 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 = ;
for (int i = ; 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 = ; 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[]) - work(Point(good_x, good_y))) < eps)
printf("%.10f\n", cal(good_x, good_y));
else {
double ans = 1e19;
for (int i = ; i < m; i++)
ans = min(ans, hehe(b[i], b[( + i) % m]));
printf("%.10f\n", ans + C);
}
return ;
}
Codeforces Round #231 (Div. 2) E.Lightbulb for Minister的更多相关文章
- CF Codeforces Round #231 (Div. 2)
http://codeforces.com/contest/394 话说这次CF做的超级不爽,A题一开始交过了,我就没再管,B题还没看完呢,就死困死困的,后来觉得B题枚举一下估计能行,当时是觉得可以从 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
随机推荐
- 字符串—strcpy
来自——百度百科 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 和 #include ...
- 形形色色的下拉菜单 (css3)
http://www.iteye.com/news/25339
- Solved Unable to copy the source file ./installer/services.sh to the destination file /etc/vmware-t
Sometimes when you intall vmwaretools there will be some problems such as "Unable to copy the s ...
- angular+ckeditor最后上传的最后一张图片不会被添加(bug)
做法一: angularJs+ckeditor 一.页面 <textarea ckeditor required name="topicContent" ng-model=& ...
- SVN 分支管理
平时在工作中使用 SVN 只是限于 commit,update 这样的操作,至多再 reslove 解决一下冲突,没有用过分支管理.开发过程中一般都是一个功能开发完成之后整体进行提交,而最近在项目中有 ...
- Java中的原始类型(Primitive Types)与引用类型(Reference Values)
Java虚拟机可以处理的类型有两种,一种是原始类型(Primitive Types),一种是引用类型(Reference Types). 与之对应,也存在有原始值(Primitive Values)和 ...
- 白话学习MVC(八)Action的执行二
一.概述 上篇博文<白话学习MVC(七)Action的执行一>介绍了ASP.NET MVC中Action的执行的简要流程,并且对TempData的运行机制进行了详细的分析,本篇来分析上一篇 ...
- iOS多线程GCD
Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...
- php +html5 websocket 聊天室
针对内容比较长出错,修改后的解码函数 和 加码函数 原文请看上一篇 http://yixun.yxsss.com/yw3104.html function uncode($str,$key){ $ma ...
- request.GetResponse 400错误处理方法
问题描述:在使用request.GetResponse时,如果是400错误,将抛出异常信息,而获取不到返回内容,所以返回的内容只能在catch上面获取,转载于 http://blog.csdn.net ...