Kattis - wheretolive 【数学--求质心】
Kattis - wheretolive 【数学】
Description
Moving to a new town can be difficult. Finding a good place to live which is close to everything you’re interested in is important. However, since you’re a great programmer, you know that you can solve this problem with an algorithm.
Everything in your virtualized town is laid out on a grid, so every place lies on an integer coordinate grid. You’ll be given a list of coordinates of the places you are interested in in the town, and you need to choose a place to live on the grid. Your program should find the grid location that minimizes the average straight-line squared distance to every place you are interested in (squared distance so that you won’t be too far from any one location).
You can live anywhere on the grid, even if something already exists where you want to live (buildings can always be built taller to accommodate you).
Input
Input consists of a list of up to 100
descriptions for towns you are considering moving to. Each town description starts with a line containing 1≤n≤1000, the number of locations you’re interested in. The next n lines each contain two space-separated integer coordinates x and y, each in the range [0,1000]. No location is repeated within a town. Input ends when n is 0
.
Output
For each town, print the location you want to live on the grid. If the best location is not exactly on a grid point, choose the grid point closest to the best location. Break ties by choosing the point that has the smallest x
coordinate and then the smallest y
coordinate.
Sample Input 1
5
82 25
25 16
97 59
38 38
15 21
9
51 13
33 2
8 46
64 25
13 40
39 75
17 42
14 6
3 43
0
Sample Output 1
51 32
27 32
题意
给出N个点的坐标,然后在这个平面内,求一个坐标使得所有点到这个坐标的距离平方和最小。
思路一
其实就是求质心。 质心就是 所有点的横坐标 求一个平均值,纵坐标求一个平均值,得出的两个值分别就是质心的横纵坐标。 为什么就是求质心呢。其实质点就是一个物体的重心。从物理上来说,就是一个物体重量最集中的地方。额 应该可以这么理解吧。那么N个点最集中的地方 大概就是质心了吧。。
但是最后如果求出来质心是一个浮点数,不能直接四舍五入。
比如 求出来是 51.4 31.8 不能直接四舍五入成 51 32 虽然这个例子 答案是对的 。但是一个正方形内某一个点到四个角的距离,哪个距离最短。。 还是四个距离都求一下 然后去最小吧。不能直接四舍五入。。
AC代码一
#include<iostream> //求质心
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
const int MAX = 0x3f3f3f3f;
const int MIN = 0xc0c0c0c0;
const int maxn = 1e3 + 5;
double f(double x1, double y1, int x2, int y2)
{
x2 = (double)x2;
y2 = (double)y2;
return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main()
{
int x, y;
int n;
while (cin >> n && n)
{
double tot_x = 0, tot_y = 0;
int i, j;
for (i = 0; i < n; i++)
{
scanf("%d%d", &x, &y);
tot_x += x;
tot_y += y;
}
tot_x /= n;
tot_y /= n;
double dis = MAX;
double MAXN = MAX;
int a[2], b[2];
a[0] = floor(tot_x), b[0] = floor(tot_y), a[1] = ceil(tot_x), b[1] = ceil(tot_y);
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
dis = f(tot_x, tot_y, a[i], b[j]);
if (dis < MAXN)
{
MAXN = dis;
x = a[i];
y = b[j];
}
}
}
printf("%d %d\n", x, y);
}
}
思路二
刚开始的做法 是想暴力枚举每一个点 因为平面范围是 [0, 1000]; 恭喜 TLE ;
然后后来想了想 因为是距离的平方和
距离公式 sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
距离的平方和 就是没有根号 就是 (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
因为没有根号 所以 X和Y 是可以单独拿出来枚举的
然后 就能A了。
AC代码二
#include<iostream> //不开平方 可以这样做
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define MAX 0x3f3f3f3f
#define MIN 0xc0c0c0c0
const int maxn = 1e3 + 5;
int x[maxn], y[maxn];
int main()
{
int n;
while (cin >> n && n)
{
double tot_x = 0, tot_y = 0;
int i, j;
int x_m[2], y_m[2];
x_m[0] = MAX;
x_m[1] = MIN;
y_m[0] = MAX;
y_m[1] = MIN;
for (i = 0; i < n; i++)
{
scanf("%d%d", &x[i], &y[i]);
if (x[i] < x_m[0])
x_m[0] = x[i];
if (x[i] > x_m[1])
x_m[1] = x[i];
if (y[i] < y_m[0])
y_m[0] = y[i];
if (y[i] > y_m[1])
y_m[1] = y[i];
}
int min_x = MAX, min_y = MAX;
int ans_x, ans_y;
for (i = x_m[0]; i <= x_m[1]; i++)
{
double dis = 0;
for (j = 0; j < n; j++)
{
dis += (x[j] - i) * (x[j] - i);
}
if (dis < min_x)
{
min_x = dis;
ans_x = i;
}
}
for (i = y_m[0]; i <= y_m[1]; i++)
{
double dis = 0;
for (j = 0; j < n; j++)
dis += (y[j] - i) * (y[j] - i);
if (dis < min_y)
{
min_y = dis;
ans_y = i;
}
}
printf("%d %d\n", ans_x, ans_y);
}
}
Kattis - wheretolive 【数学--求质心】的更多相关文章
- hiho1246(数学求模)
input 1<=n<=2000 a1 a2 ... an 1<=ai<=5*10e7 output n行,第i行指切成i段,每段和的最大公约数的最大值 做法:环形数组切成n段 ...
- 高翔《视觉SLAM十四讲》从理论到实践
目录 第1讲 前言:本书讲什么:如何使用本书: 第2讲 初始SLAM:引子-小萝卜的例子:经典视觉SLAM框架:SLAM问题的数学表述:实践-编程基础: 第3讲 三维空间刚体运动 旋转矩阵:实践-Ei ...
- 瘋子C++笔记
瘋耔C++笔记 欢迎关注瘋耔新浪微博:http://weibo.com/cpjphone 参考:C++程序设计(谭浩强) 参考:http://c.biancheng.net/cpp/biancheng ...
- 回文串--- Girls' research
HDU 3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...
- HDOJ/HDU 2547 无剑无我(两点间的距离)
Problem Description 北宋末年,奸臣当道,宦官掌权,外侮日亟,辽军再犯.时下战火连连,烽烟四起,哀鸿遍野,民不聊生,又有众多能人异士群起而反,天下志士云集响应,景粮影从. 值此危急存 ...
- 一位学长的ACM总结(感触颇深)
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...
- Kmeans在MapReduce中的实现
参考了http://www.cnblogs.com/chaoku/p/3748456.html?utm_source=tuicool的代码.不过他的代码细节上有点问题.主要在于对于质心的处理上,他的代 ...
- Spark 论文篇-论文中英语单词集
resilient [rɪˈzɪljənt] 能复原的;弹回的;有弹性的;能立刻恢复精神的;社会渣滓 dryad ['draɪæd] 森林女神 树妖 present [ˈprɛznt] 目前的;现在 ...
- ASP.NET Core 中的SEO优化(1):中间件实现服务端静态化缓存
分享 最近在公司成功落地了一个用ASP.NET Core 开发前台的CMS项目,虽然对于表层的开发是兼容MVC5的,但是作为爱好者当然要用尽量多的ASP.NET Core新功能了. 背景 在项目开发的 ...
随机推荐
- React从入门到精通系列之(14)refs和DOM元素
react.js 3.7k 次阅读 · 读完需要 8 分钟 8 十四.refs和DOM元素 在典型的React数据流中,props是父组件与其子组件交互的唯一方式. 要修改子组件,需要使用一个新的 ...
- Java and unsigned int, unsigned short, unsigned byte, unsigned long, etc. (Or rather, the lack thereof)
http://darksleep.com/player/JavaAndUnsignedTypes.html —————————————————————————————————————————————— ...
- libxl库的介绍,对Excel操作封装得很好的一个库,兼容2007版和多字节字符(最后有破解版下载)
前段时间忙着毕业论文,终于有时间写博客了. 早些时候老大给我的一个任务需要对excel进行读表操作,研究了一下c++对excel的操作. 对Excel的操作基本有com,ODBC,AD等,其中ODBC ...
- 控件禁用与启easyui用
1.validatebox可以用的用法:前两种适用于单个的validatebox;第三种应用于整个form里面的输入框; <1>.$("#id").attr(" ...
- tomcat修改默认端口
1.webserver: tomcat2.version: Apache Tomcat/7.0.293.operation: 修改默认端口 3.1 修改tomcat目录下的/conf/server ...
- css3 一个简单的静态立方体
<meta charset="utf8"> <style> .cube { position: absolute; left: 50%; top: 250p ...
- 第二百四十三节,Bootstrap模态框插件
Bootstrap模态框插件 学习要点: 1.基本使用 2.用法说明 本节课我们主要学习一下 Bootstrap 中的模态框插件,这是一款交互式网站非常常见的 弹窗功能插件. 一.基本使用 使用模态框 ...
- 第一百四十二节,JavaScript,封装库--运动动画和透明度动画
JavaScript,封装库--运动动画和透明度动画 /** yi_dong_tou_ming()方法,说明 * * yi_dong_tou_ming()方法,将一个元素,进行一下动画操作 * 1,x ...
- Axure9 v9.0.0.3629 ~ v9.0.0.3633 授权密钥 【2019.02.05】
现在提供一个支持v9.0.0.3629.v9.0.0.3630.v9.0.0.3631.v9.0.0.3632.v9.0.0.3633的授权码(后续的Beta更新版本应该能继续使用) 被授权人:zd4 ...
- 读取csv格式的数据
1.直接上代码,关键是会用 2.代码如下: <?php #添加推荐到英文站 $file = fopen('code.csv','r'); while ($data = fgetcsv($file ...