Codeforces Round #198 (Div. 2) C. Tourist Problem - 找规律
来源:互联网 发布:淘宝网网购流程图 编辑:程序博客网 时间:2024/05/21 06:56
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.
32 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 = = .
PS:虽说,喵呜(六银大神)写过了这道题目的解题报告,但是本菜还是要写这道题目的解题报告。因为这道题目虽说比赛的时候没有做出来,但是刚好在今天吃午饭的时候突然灵感迸发,想到了属于自己的解法。O(∩_∩)O~
题目大意:
在数轴上给定n个点,求从0开始走完所有这些点平均的路程。
题目分析:
给定n个点,那么有n!种走法,而且(2 ≤ n ≤ 105) 。显然不可能硬算。那么只有可能想到化简,将n!化简为O(n)的数量级最好。因为最后一步是除法,按道理是可以化简的。
这里CF的note是解题的关键
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 = = .
观察note可以得到下面的规律(不妨先设点的坐标依次为a1,a2,···,an)- |ai-0| 有(n-1)!个
- 对于ai,那么(ai-a1+ai-a2 + ··· +ai-a(i-1))有(n-1)! * 2个
#include <stdio.h>#include <algorithm>using namespace std;#define maxn 100100int a[maxn];long long gcd(long long a,long long b){ return b == 0 ? a : gcd(b,a%b);}int main(){ int n; long long sum1 = 0; long long sum2 = 0; long long sum3 = 0; scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); sort(a+1,a+1+n); for(int i = 1; i <= n; i++) { sum3 += sum1; sum1 += a[i]; sum2 += (long long)(i-1) * a[i]; } //printf("%I64d %I64d %I64d\n",sum1,sum2,sum3); long long up = sum1 + (sum2 - sum3)*2; long long down = n; long long m = gcd(up,down); printf("%I64d %I64d\n",up/m,down/m); return 0;}
PS:果然,一般题目可能会暗示解题的方向...
- Codeforces Round #198 (Div. 2) C. Tourist Problem - 找规律
- Codeforces Round #198 (Div. 2) C. Tourist Problem
- Codeforces Round #198 (Div. 2) C. Tourist Problem
- Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)
- Codeforces Round #264 (Div. 2)C(找规律)
- Codeforces Round #327 (Div. 2) (C. Median Smoothing 找规律)
- Codeforces Round #337 (Div. 2) C 找规律+构造
- Codeforces 707 C. Pythagorean Triples(找规律)——Codeforces Round #368 (Div. 2)
- Codeforces Round #259 (Div. 2) C. Little Pony and Expected Maximum (找规律)
- Codeforces Round #182 (Div. 2)---C. Yaroslav and Sequence(不错的题,分析找规律)
- Codeforces Round #316 (Div. 2) C. Replacement 找规律 或 线段树
- Codeforces Round #372 (Div. 2) -- C. Plus and Square Root(找规律枚举)
- Codeforces Round #442 (Div. 2) C. Slava and tanks 【找规律】
- Codeforces Round #338 (Div. 2) E. Hexagons 找规律
- Codeforces Round #164(Div. 2) B. Buttons【找规律】
- Codeforces Round #446 (Div. 2) D. Gluttony 找规律
- Codeforces Round #137 (Div. 2), problem: (C)
- Codeforces Round #173 (Div. 2) Problem C
- 入职之初——做事的专业精神
- windows下Bat命令学习
- 黑马程序员--Collections工具类
- SQL Server CLR全攻略
- apk文件的反编译
- Codeforces Round #198 (Div. 2) C. Tourist Problem - 找规律
- 摆脱垃圾服务商 选择微空间免费空间
- C++ vector容器类型
- oracle命令建立主键外键
- UVA 531 - Compromise(dp + LCS打印路径)
- Turbo C 2.0 图形编程出现错误 BGI Error
- extern "C"
- [ACM]18岁生日
- ios 6 屏幕适配的两种方式