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$,而且每次操作都会改变石子的奇偶性,因此按奇偶可以将状态建成二分图,然后求出最大匹配. 如果状态数是偶数,那么先手必胜,策略就是每次走匹配边, ...
随机推荐
- qrcode.js插件将你的内容转换成二维码格式
---qrcode.js插件将你的内容转换成二维码格式--- 我之前一直想知道二维码是怎么生成,所以就了解了一下, 最后还是不知道它的原理, 但是,我知道怎么生成. 现在就让我带你制作一个你喜爱的二维 ...
- 移动端下拉刷新、加载更多插件dropload.js(基于jQuery/Zepto)
移动端下拉刷新.加载更多插件dropload.js(基于jQuery/Zepto) 原文:http://www.grycheng.com/?p=1869 废话不多说,先让大家看一下案例效果: DEMO ...
- 学习Swift -- 构造器(中)
构造器(中) 值类型的构造器代理 构造器可以通过调用其它构造器来完成实例的部分构造过程.这一过程称为构造器代理,它能减少多个构造器间的代码重复. 构造器代理的实现规则和形式在值类型和类类型中有所不同. ...
- Quartz1.8.5例子(七)
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...
- mongodb常用命令【转】
mongodb由 C++编写,其名字来自humongous这个单词的中间部分,从名字可见其野心所在就是海量数据的处理.关于它的一个最简洁描述为:scalable, high-performance, ...
- iOS:不同属性声明方式的解析
代码: /* 属性声明方式说明: ----------------------- 1 @interface ... { id name } @end 这样声明的属性其实可以认为是private属性,因 ...
- Hive的MoveTask错误
最近在部署Hive上线,结果在线上线下同时出现了MoveTask报错的现象,虽然两者错误的日志以及错误信息一样,但是经过分析解决又发现两者的原因是不一样的. 首先线下的错误日志: 2015-05-18 ...
- [BZOJ 2178] 圆的面积并 【Simpson积分】
题目链接:BZOJ - 2178 题目分析 用Simpson积分,将圆按照 x 坐标分成连续的一些段,分别用 Simpson 求. 注意:1)Eps要设成 1e-13 2)要去掉被其他圆包含的圆. ...
- DJANGO不同应用之间的用户迁移
因为重新规划新的项目,数据库表结构和以前不一定了,但是想保存以前的很多用户认证方面的东东. 于是看了一下DJANGO的导入导出功能. ~~~~~~~~~~~~~~~~~~~ 数据导入: python ...
- Android 使用HTTP(get和post)方式登陆服务器
package com.wuyou.submittoserver; import android.os.Bundle; import android.support.v7.app.ActionBarA ...