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. SSH初体验系列--Hibernate--2--crud操作

    Ok,今天比较详细的学习一下hibernate的C(create).R(read).U(update).D(delete) 相关api... 前言 Session: 是Hibernate持久化操作的基 ...

  2. c# @符号后面对 双引号转义

    本文讲述c#中如何转义双引号. c#中转义双引号",使用的转义字符仍然是\. string str = "\"www.itjsxx.com\""; 但 ...

  3. Ubuntu14.4下安装FTP

    今天学习了Ubuntu上安装ftp的实例. 1.window上已经安装wamp(windows + apache + mysql + php) 2.window上已安装VMware Workstati ...

  4. vim 命令大全(写的非常全和好)http://blog.csdn.net/woshixiaosimao/article/details/54312738

    下文转自链接.由原作者陈皓译自<Learn ViM Progressively>,非常不错. ——————————正文开始—————————— 你想以最快的速度学习人类史上最好的文本编辑器 ...

  5. android 127.0.0.1/localhost connection refused,在模拟器上应该用10.0.2.2访问你的电脑本机

    调试中通过android simulator模拟器链接localhost或者127.0.0.1,因为我在电脑上面建立了apache,我的代码大概就是URL url = new URL(urlStrin ...

  6. [MongoDB]学习笔记--User管理

    1. 创建一个超级用户 use admin db.createUser( { user: "adminUserName", pwd: "userPassword" ...

  7. Linux增加swap分区的方法

    在装完Linux系统之后,建立Swap分区有两种方法. 1.新建磁盘分区作为swap分区2.用文件作为swap分区 (操作更简单,我更常用) 一.新建磁盘分区作为swap分区 1. # swapoff ...

  8. Webservice工作原理及实例

    Web Service工作原理及实例   一.Web Service基本概念   Web Service也叫XML Web Service WebService是一种可以接收从Internet或者In ...

  9. vertical-align 使用参考

    在实现文字与图片垂直对齐的时候,发现了vertical-align的值也是很纷繁复杂,博客讲解各种不懂,最终还是找了CSS权威指南-中文第三版,看了前6章,终于弄明白了,如果你也有不懂的地方,请去看书 ...

  10. C# 矩阵乘法实现

    矩阵乘法是一种高效的算法可以把一些一维递推优化到log( n ),还可以求路径方案等,所以更是是一种应用性极强的算法.矩阵,是线性代数中的基本概念之一.一个m×n的矩阵就是m×n个数排成m行n列的一个 ...