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后和给 ...
随机推荐
- Thinkphp中自己组合的数据怎样使用框架的分页
做项目有时候,需要自己处理组合数据,不是直接从表中提取出来的.不能按照手册得方法分页显示数据.这时候就得想办法,正好看到他人的方法.地址为:http://www.thinkphp.cn/code/27 ...
- .net下Ueditor配置(主要讲解上传功能配置)
Ueditor上传功能配置——.net 今天项目中用到Ueditor就自己配置了下,但上传功能和图片显示功能不能正确使用,就自己琢磨了下.已实现上传功能,并能显示正确的图片和文件,在线编辑功能也能使用 ...
- bzoj3594: [Scoi2014]方伯伯的玉米田--树状数组优化DP
题目大意:对于一个序列,可以k次选任意一个区间权值+1,求最长不下降子序列最长能为多少 其实我根本没想到可以用DP做 f[i][j]表示前i棵,操作j次,最长子序列长度 p[x][y]表示操作x次后, ...
- android之消息机制(二)
消息通道:Looper 首先编写main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk ...
- BizTalk开发系列(三十五) TCP/IP 适配器
BizTalk 的TCP/IP适配器最初是为英国的保健行业开发.该适配器属于BizTalk进程内适配器,将消息通过TCP/IP 套接字符串在BizTalk服务器与远程客户端间进行通讯. TCP/IP适 ...
- Android课程---Activity中保存和恢复用户状态
onSaveInstanceState 保存 在暂停之后和保存之前调用 onRestoreInstanceState 恢复 再启动之后和显示之前调用 package com.example.chens ...
- Unity学习疑问记录之新GUI
学习Unity 4.6新GUI系统 http://segmentfault.com/a/1190000000642686
- nodejs 初入
nodejs 模块路径 1.内置模块 如果传递给require函数的是NodeJS内置模块名称,不做路径解析,直接返回内部模块的导出对象,例:require('http'). 2. nodejs 支 ...
- SQL基础巩固2
日期函数 函数名称 含义 示例 GetDate 返回当前系统日期和时间,返回值类型为datetime select GETDATE()//输出当前日期 YEAR 返回指定日期的年份 YEAR('08/ ...
- 设置SQLServer的行版本控制隔离级别
1.--查询数据库状态 select name,user_access,user_access_desc,snapshot_isolation_state,snapshot_isolation_sta ...