uva10041
2015-12-30 17:33
274 查看
题目大意:
求vito到各个邻居家的距离和要最小,最小为多少。
思路:
本来想说求平均数的,可惜WA了,后来想了个样例,1 2 2 2 2 2 ,
11/6 = 1的,其实要选择住在2距离才会最短。所以这题要求的是中位数。
代码:
求vito到各个邻居家的距离和要最小,最小为多少。
思路:
本来想说求平均数的,可惜WA了,后来想了个样例,1 2 2 2 2 2 ,
11/6 = 1的,其实要选择住在2距离才会最短。所以这题要求的是中位数。
代码:
#include <iostream> using namespace std; #include <cstring> #include <stdio.h> #include <cmath> #include <algorithm> int s[30002]; int main() { int n,t; int sum; scanf("%d",&n); while(n--) { sum = 0; scanf("%d",&t); for(int i = 0 ; i < t ; i++) { scanf("%d",&s[i]); } int m; sort(s,s+t); if(!(t%2)) { m = s[t / 2 - 1]; } else m = s[t / 2]; for(int i = 0; i < t; i++) sum = sum + abs(s[i] - m); printf("%d\n",sum); } return 0; }
相关文章推荐
- Android常用正则工具类
- requireJS配置选项
- 第十一周 二叉树的构造
- enum 类型
- Linux_Network_Management
- 解决ubuntu Ctrl Alt F1界面中文乱码的问题
- 分区水位的释放
- ping: unknown host 解决办法
- 【iOS】导航、Tabbar、tableview配合使用时,tableview错位的问题
- web.xml中<security-constraint>和四种认证类型
- Eclipse闪退、adb无法打开
- Linux Bridge实践 -- 使用dnsmasq为虚拟机分配IP地址
- 数据库链接 mysql,sqlserver
- jQuery源码
- LeetCode Search in Rotated Sorted Array
- Android 5.0新控件——FloatingActionButton(悬浮按钮)
- NSURLConnection基本使用
- 在Android中通过WebView调用相机拍照/选择文件
- unity生成android release 版本的apk性能比较好
- Android 5.0新控件——FloatingActionButton(悬浮按钮)