Codeforces Round #198 (Div. 2) C. Tourist Problem
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 = = .
我们可以找出规律,答案就是(sum{pri[i]}+任意两点间的距离)/n,而任意两点间的距离我们不能用n*n来枚举,我们可以找出公式sum{(p[i]-p[i-1])*i*(n-i)*2};这样,排完序之后,就可以用线性时间内a掉了!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define M 100500
__int64 pri[M];
__int64 gcd(__int64 a,__int64 b){
if(a==0)return b;
return gcd(b%a,a);
}
bool cmp(int a,int b){
return a<b;
}
int main()
{
__int64 n,sum,s,k,tempn;
int i,j;
while(scanf("%I64d",&n)!=EOF){
for(sum=0,s=0,i=0;i<n;i++){
scanf("%I64d",&pri[i]);
sum+=pri[i];
}
sort(pri,pri+n,cmp);
for(s=0,i=1;i<n;i++){
s+=(pri[i]-pri[i-1])*i*(n-i);
}
s*=2;
k=gcd(s+sum,n);
printf("%I64d %I64d\n",(s+sum)/k,n/k);
}
return 0;
}
最新文章
- Android中查找一个Layout中指定的子控件
- oracle修改sys用户密码
- linux:/etc/rc.local 不能自动启动问题
- 【你吐吧c#每日学习】11.10 C# Data Type conversion
- Javascript 基础--数组
- mysql_upgrade命令
- iOS语言国际化
- Kinetic使用注意点--animation
- RPM包制作最简单样例
- javascript 中 undefined 和 null 区别
- Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Multiple representations of the same entity解决方法
- c#中如何跨线程调用windows窗体控件?
- Xcode 断点无用,也不打印输出
- spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property
- C#一例绘制字体不清晰的解决办法
- UICollectionViewController的用法1
- openFileDialog的使用
- 【手记】解决“未能创建 SSL/TLS 安全通道”异常
- Delphi: 模态窗体最小化
- [置顶] js 实现 <;input type=";file"; />; 文件上传