题意:有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的更多相关文章

  1. CF Codeforces Round #231 (Div. 2)

    http://codeforces.com/contest/394 话说这次CF做的超级不爽,A题一开始交过了,我就没再管,B题还没看完呢,就死困死困的,后来觉得B题枚举一下估计能行,当时是觉得可以从 ...

  2. 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 ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

随机推荐

  1. Python字符串,元组、列表、字典

    1.字符串 <string>.strip() 去掉两边空格及去指定字符 <string>.split() 按指定字符分隔字符串为数组 <string>.isdigi ...

  2. 使用dd制作文件夹的img

    du -sh originfile //先看看需要制作的源文件夹大小,假如15M dd if=/dev/zero of=new_img.img bs=1024 count=20000 //生成20M的 ...

  3. 杭电ACM 1998奇数阶魔方

    #include<stdio.h>#include <string.h>int main(){ int n,m; int a[40][40]={0}; scanf(" ...

  4. IOS第四天(2:字典转模型plist)

    HMQuestion.h #import <Foundation/Foundation.h> @interface HMQuestion : NSObject @property (non ...

  5. lr并发量和迭代的区别

    1.并发量 并发量也就是同时运行的量.比如100个用户同时登录,那么并发量就是100.当然这100个用户可以进行参数化,也可以采用设置虚拟用户数(vuser). 2.迭代 迭代就是单个用户运行的次数. ...

  6. Linq 动态查询排序

    Linq的排序一般是这样写的: query.OrderBy(x => x.Tel).Skip().Take(); 实际使用中排序字段可能是通过字符类型的参数来设置的,于是想这样实现: query ...

  7. iOS开发中常用到的宏定义

    1.首次启动判断: #define First_Launched @"firstLaunch" 2.ios7系统判断: #define IsIOS7 ([[[UIDevice cu ...

  8. 发布Mvc 项目 mvc.dll版本号不一致 出现 3.0.0.1 和4.0.0.1的解决方案

    部署Mvc 项目的时候,很可能出现以下情况的提示: System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856a ...

  9. sublime 常用插件

    1.ConvertToUTF8 支持 GBK, BIG5, EUC-KR, EUC-JP, Shift_JIS 等编码的插件 2.Bracket Highlighter 用于匹配括号,引号和html标 ...

  10. [转]Android Studio系列教程六--Gradle多渠道打包

    转自:http://www.stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/ Android Studio系列教程六--Grad ...