C. Tourist Problem

Codeforces Round #198 (Div. 2) C

C. Tourist Problem

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 sequence a1, 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.

Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, …, an (1 ≤ ai ≤ 107).

Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.

Examples
input
3
2 3 5
output
22 3

link C

补题补题 , 菜的毫无思路,疯狂观察两位大佬的思路,按自己的理解写了一哈分析

分析:
1.从n个点里面选出n个点作为路线,所以总的方案数为全排列 n!

2.具体分为两个步骤思考:

①从起点到第一步的距离为 (a1+a2+a3+…+an) * (n-1)!
//去掉第一个点,剩下的点全排列,所以为(n-1)! , 第一步的方案为 第一个点的所有可能 * 剩下的点全排列 相加

②任意两点之间 i,j 的距离为2 * ∑ |ai-aj|*(n-1)!
//去掉两点,剩下的点的全排列有(n-2)! 种情况,任意一个点有n-1个位置可放,所以为(n - 1) !
则任意两点距离为 ( ai - aj ) * (n - 1)! ,则距离总和为所有距离的和,而又包括 例如 a1 - > a2 与 a2 - > a1 的情况 ,所以 * 2

3.则总距离为 ① + ② ,2 * ∑ |ai-aj|(n-1)! + ∑ ai * (n - 1)!
又因 题目要求 求平均距离 所以为 总距离 / 总路线数 即除以 n !
结果为 ( ∑ ai + 2
∑|ai-aj| ) / n;

4.直接枚举计算 ∑|ai-aj| 数据范围过大 会超时,因此,观察可知
a1 - > a3 可有 a1 - > a2 + a2 - > a3 组成与 a2 - > a3
所以计算任意两端的距离 为 Si = S(i-1)+ i * |ai - a(i+1)|
// 例如 a4 可有 a1 - > a3 + a2 - > a3 + a3 - > a3 + 3 *( a4 - > a5) 的距离

5.gcd 求出 ans 与 n 的最大公约数 ,化简

代码:

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;

int main()
{
	long long n,a[N],count = 0;
	cin >> n;
	for(int i = 1;i <= n;i ++)
	{
	cin >> a[i];
	count += a[i];  // 求 ∑ ai 
    }
    
	a[0] = 0;  // 起点为 0 ,易计算第一个点的距离
	sort(a,a+n+1);
	long long ans = 0,s = 0 ;
	
	for(int i = 1;i < n;i ++)
	{
	s += i*(a[i+1] - a[i]);
	ans += 2*s;
    }
    
	long long x = __gcd(ans + count , n);
	
	cout << (ans + count)/x << ' ' << n / x << endl;
	
	return 0;
} 

大佬文章链接
link 1
link 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值