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 【数学--求质心】的更多相关文章

  1. hiho1246(数学求模)

    input 1<=n<=2000 a1 a2 ... an 1<=ai<=5*10e7 output n行,第i行指切成i段,每段和的最大公约数的最大值 做法:环形数组切成n段 ...

  2. 高翔《视觉SLAM十四讲》从理论到实践

    目录 第1讲 前言:本书讲什么:如何使用本书: 第2讲 初始SLAM:引子-小萝卜的例子:经典视觉SLAM框架:SLAM问题的数学表述:实践-编程基础: 第3讲 三维空间刚体运动 旋转矩阵:实践-Ei ...

  3. 瘋子C++笔记

    瘋耔C++笔记 欢迎关注瘋耔新浪微博:http://weibo.com/cpjphone 参考:C++程序设计(谭浩强) 参考:http://c.biancheng.net/cpp/biancheng ...

  4. 回文串--- Girls' research

    HDU   3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...

  5. HDOJ/HDU 2547 无剑无我(两点间的距离)

    Problem Description 北宋末年,奸臣当道,宦官掌权,外侮日亟,辽军再犯.时下战火连连,烽烟四起,哀鸿遍野,民不聊生,又有众多能人异士群起而反,天下志士云集响应,景粮影从. 值此危急存 ...

  6. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  7. Kmeans在MapReduce中的实现

    参考了http://www.cnblogs.com/chaoku/p/3748456.html?utm_source=tuicool的代码.不过他的代码细节上有点问题.主要在于对于质心的处理上,他的代 ...

  8. Spark 论文篇-论文中英语单词集

    resilient [rɪˈzɪljənt] 能复原的;弹回的;有弹性的;能立刻恢复精神的;社会渣滓 dryad ['draɪæd] 森林女神 树妖 present [ˈprɛznt]  目前的;现在 ...

  9. ASP.NET Core 中的SEO优化(1):中间件实现服务端静态化缓存

    分享 最近在公司成功落地了一个用ASP.NET Core 开发前台的CMS项目,虽然对于表层的开发是兼容MVC5的,但是作为爱好者当然要用尽量多的ASP.NET Core新功能了. 背景 在项目开发的 ...

随机推荐

  1. deep learning+ Depth Estimation

    Depth estimation/stereo matching/optical flow @CVPR 2017 Unsupervised Learning of Depth and Ego-Moti ...

  2. 【vijos】1769 网络的关键边(割边)

    https://vijos.org/p/1769 啊,割边写挫了害得我交了那么多发... 本题多想想就出来了.. 首先求出割边,显然关键边就在割边上. 求完割边后,我们先从一个点dfs,维护A的点数和 ...

  3. 快速开发微信小程序

    image.png 最近婷主在做微信小程序.自己的微信公众号也需要添加点料,乘着这次放假,把微信小程序研究了下.虽然没有做什么很强大的功能,不过好歹自己的公众号也有了微信小程序.够用即可. 1.需要先 ...

  4. 【watcher】 #02 c# 中实现时间戳等,日期数字及大概率绝对随机数 实现

    在Wacher的项目中,用到了很多时间记录的地方,为了将来能够和在线数据打通,我们使用了时间戳来记录时间信息 由于c# 没有现成的方法,所以我们重新写了一个Helper类来帮助我们使用这些公共函数 同 ...

  5. Ideal-image-slider 幻灯片实例演示

    链接:http://zaixianshouce.iteye.com/blog/2316300 http://www.shouce.ren/study/api/s/jq--5733e32bf23bb-- ...

  6. Android音频文件浏览+音频播放

    该Demo执行后,会显示全部你sd卡上的音乐文件列表, 并能够点击列表选择某一首歌曲进行播放. 执行效果: 点击download出现: 然后点击歌曲调用系统播放器播放. 源码: activity_au ...

  7. java web 运动前端

    [写在前面的话:]前不久刚看到过一句话:说好的技术文章应该让读者感觉增加信心,而不是失去信心.有感于这句话是因为以前觉得发一些貌似高深的,看起来nb的东西才算一篇好博文,可是多少有点炫技的成分.可是后 ...

  8. gcc/g++实战之动态链接库与静态链接库编写

    函数库一般分为静态库和动态库两种. 静态库: 是指编译链接时,把库文件的代码全部加入到可执行文件中,因此生成的文件比较大,但在运行时也就不再需要库文件了.其后缀名一般为”.a”. 动态库: 与之相反, ...

  9. String, JSONArray , JSONObject ,Map<String, Object> 与对象

    String pic = "[{\"picServiceUrl\": \"0f4bb44afb2e48d48b786d3bbdeec283/20180408/6 ...

  10. bunoj 13124(数位dp)

    数位dp每次都给我一种繁琐的感觉.. A - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO F ...