C. Tourist Problem
http://codeforces.com/problemset/problem/340/C
赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容易受外界影响
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
=
=
.
/*
分析:由si产生的值可以分为以下两种情况
1.对于si来说,si放到最左端构成(n-1)!个排列,由si构成的总和为si*(n-1)!,有n个数
2.对于si来说,si放到sj的左端,则每一次的位置有(n-2)!个排列,有n-1个位置可放,由si构成的总和为|si-sj|*(n-1)!,有n个数
综合1,2可得总和为:
si*(n-1)!*n/((n-1)!*n) + |si-sj|*(n-1)!*n/((n-1)!*n) = ( si+|si-sj| )/n;//i=1...n,j=1...n
最后的难点就是如何去计算|si-sj|,由于si-sj不确定正负,所以可以事先对s进行排序,然后拆开|si-sj|得到:
s1-s1 + s2-s1 + s3-s1 + s4-s1 + s5-s1 +...+ sn-s1
+
s2-s1 + s2-s2 + s3-s2 + s4-s2 + s5-s2 +...+ sn-s2
+
s3-s1 + s3-s2 +s3-s3 + s4-s3 + s5-s3 +...+ sn-s3
...
=i*si-sum[i]+(sum[n]-sum[i])-(n-i)*si=sum[n]-2*sum[i]+(2*i-n)*si;//sum[i]表示前i个数的和
其中2*sum[i]的总和为2(n-i+1)*si;//i=1...n
最终推出ans+=(4*i-2*n-1)*si
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#include<cmath>
#include<iomanip>
#define INF 999999999
using namespace std; const int MAX=100000+10;
__int64 s[MAX],sum,n; int main(){
while(cin>>n){
sum=0;
for(int i=1;i<=n;++i)cin>>s[i];
sort(s+1,s+n+1);
for(int i=1;i<=n;++i){
sum+=(4*i-2*n-1)*s[i];
}
__int64 gcd=__gcd(sum,n);
cout<<sum/gcd<<' '<<n/gcd<<endl;
}
return 0;
}
C. Tourist Problem的更多相关文章
- codeforces 340C Tourist Problem(公式题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Tourist Problem Iahub is a big fan of tou ...
- Codeforces Round #198 (Div. 2) C. Tourist Problem
C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)
C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- CodeForces - 340 C - Tourist Problem
先上题目: A - Tourist Problem Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- codeforces 340C Tourist Problem
link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...
- cf C. Tourist Problem
http://codeforces.com/contest/340/problem/C #include <cstdio> #include <cstring> #includ ...
- C. Tourist Problem 2021.3.29 晚vj拉题 cf 1600 纯数学题
拉题链接 https://vjudge.net/contest/430219#overview 原题链接 https://codeforces.com/problemset/problem/340 ...
- codeforces 340C Tourist Problem(简单数学题)
题意:固定起点是0,给出一个序列表示n个点,所有点都在一条直线上,其中每个元素代表了从起点到这个点所走的距离.已知路过某个点不算到达这个点,则从起点出发,到达所有点的方案有许多种.求所有方案走的总路程 ...
- XIII Open Cup named after E.V. Pankratiev. GP of Saratov
A. Box Game 注意到局面总数不超过$50000$,而且每次操作都会改变石子的奇偶性,因此按奇偶可以将状态建成二分图,然后求出最大匹配. 如果状态数是偶数,那么先手必胜,策略就是每次走匹配边, ...
随机推荐
- mysql备份和恢复
摘自:http://safe.it168.com/a2009/1108/805/000000805490.shtml 要备份数据库" phpbb_db_backup " #mysq ...
- IS脚本学习
OnFirstUIBefore:函数块用于第一安装应用时安装部件前所要完成的任务.一般在这里进行下列设: 1. 设置屏蔽 2. 显示欢迎信息,软件协议书或关于软件安装的其他说明信息 3. 从用户处获取 ...
- 帝国cms在任意位置调用指定id的栏目名称和链接
注意,这个代码无须放在灵动标签中,直接写入模板相应的位置就行了.[1]调用栏目名称: <?=$class_r[栏目ID]['classname']?> 示例:<?=$class_ ...
- linux无法umount解决方案
[root@995120-master ~]# umount /drbd/ umount: /drbd: device is busy.(In some cases useful info about ...
- 2016021903 - 下载安装使用Memory Analyzer
Memory Analyzer是做什么的? 分析java程序中分析内存泄露问题. 1.下载Memory Analyzer Memory Analyzer下载地址:http://www.eclipse. ...
- Android 开发环境搭建9传送帖)
---恢复内容开始--- 首先,得安装软件,感觉我所找到的一些课本上写的都比较简略,走过一些弯路后,决定按照这个百度经验来 Android安卓开发环境搭建详细教程 http://jingyan.bai ...
- bzoj 3287: Mato的刷屏计划 高精水题 && bzoj AC150
3287: Mato的刷屏计划 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 124 Solved: 43[Submit][Status] Desc ...
- lc面试准备:Invert Binary Tree
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...
- Lua function 函数
Lua支持面向对象,操作符为冒号‘:’.o:foo(x) <==> o.foo(o, x). Lua程序可以调用C语言或者Lua实现的函数.Lua基础库中的所有函数都是用C实现的.但这些细 ...
- 怒刷BZOJ记录(二)1038~10xx
我实在是太弱了...不滚粗只能刷BZOJ了...这里来记录每天刷了什么题吧. 2015-8-13: 正式开始! 1030[JSOI2007]文本生成器 | ...