UVa 10041 - Vito's Family
【链接】
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=982
【原题】
Background
The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them living in Lamafia Avenue. Since he will visit all his relatives very often, he is trying to find a house close to them.
Problem
Vito wants to minimize the total distance to all of them and has blackmailed you to write a program that solves his problem.
Input
The input consists of several test cases. The first line contains the number of test cases.
For each test case you will be given the integer number of relatives r ( 0 < r < 500) and the street numbers (also integers) where they live ( 0 < si < 30000 ). Note that several relatives could live in the same street number.
Output
For each test case your program must write the minimal sum of distances from the optimal Vito's house to each one of his relatives. The distance between two street numbers s i and s j is d ij = | s i - s j |.
Sample Input
2 2 2 4 3 2 4 6
Sample Output
2 4
【题目大意】
一个黑社会老大要搬家到纽约的某一条街上, 他在那条街上有很多的亲戚朋友,要找到一个地方,使得这个地方走到所有亲戚朋友家的总距离最短。
【分析与总结】
赤裸裸的找中位数就OK了...
【代码】
/** UVa: 10041 Vito's Family* Time: 0.024s* Author: D_Double**/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define MAXN 510
using namespace std;
int arr[MAXN], n;void solve(){sort(arr, arr+n);if(n&1){ //如果是奇数,一定是正中间那个数int mid=arr[(n-1)>>1];int sum=0;for(int i=0; i<n; ++i)sum += abs(arr[i]-mid);printf("%d\n",sum);}else{ //如果是偶数,那么是中间两个之和的一半int mid=(arr[(n-2)>>1]+arr[n>>1])/2;int sum=0;for(int i=0; i<n; ++i)sum += abs(arr[i]-mid);printf("%d\n", sum);}
}int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=0; i<n; ++i)scanf("%d",&arr[i]);solve();}return 0;
}
—— 生命的意义,在于赋予它意义。
UVa 10041 - Vito's Family相关推荐
- UVA 10041 Vito's Family
UVA_10041 这个题目是一个贪心的题目. 如果设按升序排列的si的数组为s[],那么Vito的位置一定为s[(r-1)/2].对于这一点,我们分两种情况进行讨论: ①如果si的数量为奇数,那么V ...
- UVA - 10041 Vito's Family
题目大意:给出所有邻居的位置,要求找到一个位置,使得该位置到所有邻居家的总和最小 解题思路:单纯的找中位数,先排序, 然后将数组遍历一遍求的最小值 #include <cstdio> #i ...
- UVa Problem 10041 Vito’s Family (Vito 家族)
// Vito's Family (Vito 家族) // PC/UVa IDs: 110401/10041, Popularity: A, Success rate: high Level: 1 / ...
- π-Algorithmist分类题目(1)
原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(1) Sorting UVAL ...
- sicily题目分类
sicily题目分类 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. ...
- [sicily]部分题目分类
sicily题目分类 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. ...
- Sicily 题目分类
依照自己水平挑着做→ →~~ 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 ...
- 编程题目分类(剪辑)
1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代 ...
- UVa Online Judge 工具網站
UVa Online Judge 工具網站 转自http://www.csie.ntnu.edu.tw/~u91029/uva.html Lucky貓的ACM園地,Lucky貓的 ACM 中譯題目 M ...
最新文章
- UML for Java Programmers之dx实战
- 怎样使一个Android应用不被杀死?(整理)
- 如何利用C#开发“扫雷”小游戏
- 重复提交版本之后该采取的测试策略
- Python Numpy介绍
- 云来重置任意用户密码
- 10th blog:Object
- 使用DatagramSocket与DatagramPacket传输数据
- .Net环境下基于Ajax的MVC方案
- myelicpes2019初次使用设置_实况足球2019球员数据编辑器怎么使用
- linux 迁移类型,迁移到Linux前必须要知道的五个方面
- 计算机语言中beta是什么意思,Tea语言迎来1.0第一个Beta版本
- ASP.NET MVC5中局部视图的更新
- 前端JS获取ModelMap的值 2020-09-03
- 重磅:微信官方推出 Web 前端和小程序统一框架
- Codeforces 85D Sum of Medians(线段树)
- 时间弥散谱成像和IMPULSED
- 计算机学院年会,我校计算机学院应邀出席全国职业院校计算机系主任年会并作报告...
- scrapy 保存到mysql_scrapy爬虫保存数据到mysql
- 面试问题:2018年全国大学生数学建模竞赛项目(RGV动态调度模型、贪婪算法)