A. The New Year: Meeting Friends

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?

It's guaranteed that the optimal answer is always integer.

Input

The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.

Output

Print one integer — the minimum total distance the friends need to travel in order to meet together.

Examples

Input

7 1 4

Output

6

Input

30 20 10

Output

20

Note

In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.

____________________

题意:给定a,b,c三个点,在数轴上找出一点p使他们距离之和sum最小,并输出最小的距离之和sum。

一开始直觉感觉是三个数平均数,交上去Wrong Answer on test 3 (test3是 1 4 100 ) 。

于是写了个暴力过了(毕竟答案肯定是整数,数据范围又小的可怜):

#include<iostream>
#include<cmath>
//#include<cstdlib>
using namespace std ; int main()
{
int a,b,c;
int min = 101 ;
cin >> a >> b >> c ;
int temp = 0 ;
for ( int i = 1 ; i <= 100 ; i++ )
{
temp = abs( a - i ) + abs ( b - i ) + abs( c - i );
if(temp < min ) min = temp ;
}
cout << min << endl ;
return 0 ; }

————

但我认为该题有数学方法并且可以推广,不需要写这么不优雅的暴力。

将该题推广如下:

在数轴上给定n个点,找一点p使p到各点距离之和最小。

——

经过一番讨论,发现p是n个点的中位数。

修改代码如下:

#include<iostream>
#include<cmath> using namespace std ;
void swap( int &a , int &b)
{
int temp ;
temp = a ;
a = b ;
b = temp ;
}
int main()
{
int a,b,c;
cin >> a >> b >> c ;
if( a > b ) swap(a,b);
if(a > c ) swap(a,c);
if( b>c) swap(b,c);
cout << abs(a-b)+abs(c-b) << endl ; return 0 ; }

————————

对于n个点的证明:

假设p点不是那个点的中位数,则p点左边有m个数,右边有n个数(m≠n) 。

那么如果将p点向左移动d(但没有使p两边数的个数发生改变),则左边距离减少md,右边增加距离nd。右边移动同理。

因此,当p点左右两边数的个数不一样的时候,不是最优解。

所以p是中位数。(这里貌似不太严谨啊)

如果n是奇数,则p = (n + 1 ) /2

如果n是偶数,则p是在 ( n/2 , n/2 + 1 ) 上任意一点。

Codeforces Round #375 (Div. 2)A. The New Year: Mee的更多相关文章

  1. Codeforces Round #375 (Div. 2) - D

    题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...

  2. Codeforces Round #375 (Div. 2) - C

    题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...

  3. Codeforces Round #375 (Div. 2) - B

    题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...

  4. Codeforces Round #375 (Div. 2) - A

    题目链接:http://codeforces.com/contest/723/problem/A 题意:在一维坐标下有3个人(坐标点).他们想选一个点使得他们3个到这个点的距离之和最小. 思路:水题. ...

  5. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

  6. Codeforces Round #375 (Div. 2) E. One-Way Reform 欧拉路径

    E. One-Way Reform 题目连接: http://codeforces.com/contest/723/problem/E Description There are n cities a ...

  7. Codeforces Round #375 (Div. 2) D. Lakes in Berland 贪心

    D. Lakes in Berland 题目连接: http://codeforces.com/contest/723/problem/D Description The map of Berland ...

  8. Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟

    B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...

  9. Codeforces Round #375 (Div. 2) A. The New Year: Meeting Friends 水题

    A. The New Year: Meeting Friends 题目连接: http://codeforces.com/contest/723/problem/A Description There ...

随机推荐

  1. NOIP2013-普及组初赛C语言解析

    第十九届全国青少年信息学奥林匹克联赛初赛 一.单项选择题(共 20 题,每题 1.5 分,共计 30 分.每题有且仅有一个正确选项)1. 一个 32 位整型变量占用(A )个字节.A. 4 B. 8 ...

  2. <蛇形填数>--算法竞赛 入门经典(第2版)- 3.1 数组 程序3-3 蛇形填数

     蛇形填数: 在n×n方阵里填入1,2,....,n×n,要求填成蛇形.例如,n = 4 时方阵为:    10  11  12  1   9  16  13  2 8  15  14  3 7   ...

  3. nginx的https环境如何配置

    http://www.cnblogs.com/yanghuahui/archive/2012/06/25/2561568.html http://www.fzb.me/2015-1-15-openss ...

  4. sap 设备cnsapwin不支持页格式*****

    SAP SMARTFORMS 打印 CNSAPWIN 不支持页格式 解决办法: 在smartforms里的表格属性虽然定义了要打印的页格式 ZUNIA5 ,但是打印时会提示错误:" CNSA ...

  5. 【实验室笔记】C#以本地时间创建txt文件

    前段时间做的一个小项目,要求上位机在打开时候,以打开软件的系统时间的建立一个txt文件来存储下位机发送来的数据. 在第一版上位机上,取名的办法太弱了,先是读取系统时间,然后截取字符串,太笨拙.昨天,查 ...

  6. 逆向思维 UVA 11853

    题目大意:紫书175 思路:看书...2333 关键点就是利用已知条件来逆向思考是否能走通,而不是傻傻的从某个点开始出发啊啥的.

  7. ubuntu14.04下安装ice3.5.1

    ubuntu 14.04下是可以通过下面这条指令安装ice3.5的 sudo apt-get install libzeroc-ice35-dev 1. 从这里下载ice源文件 主要包括两部分:ice ...

  8. Python基础篇-day8

    本节目录1.抽象接口2.静态方法.类方法.属性方法3.类的特殊方法 3.1 __doc__ 表示类的描述信息(注释) 3.2 __module__ 和 __class__ 3.3 __init__ 构 ...

  9. 将可执行exe文件注册成windows服务

    要把应用程序添加为服务,你需要两个小软件:Instsrv.exe和Srvany.exe.Instsrv.exe可以给系统安装和删除服务,Srvany.exe可以让程序以服务的方式运行.这两个软件都包含 ...

  10. 常用几种Java Web容器

    Web服务器是运行及发布Web应用的容器,只有将开发的Web项目放置到该容器中,才能使网络中的所有用户通过浏览器进行访问.开发Java Web应用所采用的服务器主要是与JSP/Servlet兼容的We ...