Codeforces Round #198 (Div. 2) 340C
1 second
256 megabytes
standard input
standard output
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequencea1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
3
2 3 5
22 3
Consider 6 possible routes:
- [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
 - [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
 - [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
 - [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
 - [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
 - [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
 
The average travel distance is 
 = 
 = 
.
题意:求出所有走的方案的平均花费。。
思路:先找出规律。我们发现,num中每两个组合都会出现(n - 1)!次,而分母是n!次,所以抵消,分母为n,分子为求出每个数字和其他数字(以及0)的差的绝对值之和即可,直接暴力枚举为O(n^2)会超时。所以这样:先把num从小到大排序,然后算出总和,然后用一个now来记录第i个前i个和,然后总和为num[i] * i - now (i的前半部分) + sum - now -num[i] * (n - i) - num[i](后半部分),这样一来时间复杂度只有O(n)。
要注意的是要用longlong。由于当时编译器只有渣渣vc6.0所以就用__int64了。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int main() {
__int64 sum = 0, now = 0, ans = 0, num[100005], n, i;
num[0] = 0;
scanf("%I64d", &n);
for (i = 1; i <= n; i ++) {
scanf("%I64d", &num[i]);
sum += num[i];
}
sort(num, num + 1 + n);
for (i = 1; i <= n; i ++) {
ans += 2 * num[i] * i - 2 * now + sum - num[i] * (n + 1);
now += num[i];
}
__int64 a, b;
a = ans; b = n;
if (a < b) {
__int64 t = b;
b = a;
a = t;
}
while (b) {
__int64 sb = b;
b = a % b;
a = sb;
}
printf("%I64d %d\n", ans / a, n / a);
return 0;
}
Codeforces Round #198 (Div. 2) 340C的更多相关文章
- Codeforces Round #198 (Div. 2)A,B题解
		
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
 - [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)
		
http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...
 - Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理
		
题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...
 - Codeforces Round #198 (Div. 1)  D. Iahub and Xors 二维树状数组*
		
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
 - Codeforces Round #198 (Div. 2)
		
A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...
 - Codeforces Round #198 (Div. 1) B,C 动态规划
		
比赛时,开了大号去做,算了半天发现不会做A,囧.于是跑去看B,发现很水?于是很快敲完了,但是A不会,没敢交.于是去看C,一直找规律啊,后来总算调了出来,看了一下榜,发现还是算了吧,直接去睡觉了.第二天 ...
 - Codeforces Round #198 (Div. 2) —— D
		
昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...
 - Codeforces Round #198 (Div. 2) —— C
		
C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...
 - Codeforces Round #198 (Div. 2) —— B
		
B题是一个计算几何的题,虽然以前看过计算几何的ppt,但一直都没有写过: 昨晚比赛的时候本来想写的,但是怕不熟练浪费时间,太可惜了! 其实没必要选出一个最大的矩形: 以矩形的一条对角线为轴,向上或者向 ...
 
随机推荐
- openstack 源码分析
			
Nova对于底层Hypervisor(如KVM/QEMU等)的调用与管理主要通过LibvirtDriver类,nova/virt/libvirt/driver.py Libvirt对Hyperviso ...
 - ##DAY6 UIScrollView
			
##DAY6 UIScrollView #pragma mark ———————UIScrollView——————————— 属性: contentSize 内容滚动范围 contentOffset ...
 - MysqlHelp
			
using System.Configuration;using MySql.Data: public class MySqlHelp { //链接字符串 private static string ...
 - shell学习-读取输入
			
功能:读取输入,打印:如果长度小于MINLEN,那么输出空格. #!/bin/bash # paragraph-space.sh # Insert a blank line between parag ...
 - NET Core个人博客
			
NET Core重写个人博客站点小结 今天用ASP.NET Core重写了个人博客站点,原来是基于ASP.NET 4.5开发的.重写工作总体很顺利,最后成功发布到Ubunt+Nginx平台上.效果如下 ...
 - sql语法复习:增删查改,各种数据库对象创建和函数使用
			
推荐工具:机子配置较低的话,可以装Gsql这个工具获得sql执行环境(可作为手册查看内置数据类型 函数和存储过程等) --之前数据库的东西接触不多,虽然基本的语法是了解,但不是很熟悉--最近项目一直在 ...
 - ISO/IEC 14443协议浅谈
			
一. 非接触IC卡简介 非接触IC卡又称射频卡,是射频识别技术和IC卡技术有机结合的产物.它解决了无源(卡中无电源)和免接触这一难题,具有更加方便.快捷的特点,广泛用于电子支付.通道控制.公交收费.停 ...
 - 使用 Windows Media Center 远程控制
			
http://windows.microsoft.com/en-us/windows/getting-started-windows-media-center#getting-started-wind ...
 - HDU2577:How to Type(DP)
			
Problem Description Pirates have finished developing the typing software. He called Cathy to test hi ...
 - C++模板:二分查找
			
bool find(int x,int l,int r){ if(l>r)return false; int mid=(l+r)/2; if(s[mid]==x) return true; el ...