CodeForces - 340 C - Tourist Problem
先上题目:
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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.
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.
Sample Input
3
2 3 5
22 3 题意:给你一维上的一些点,坐标用自然数表示,起点为0,根据不同的遍历顺序遍历这些点(这些点不重复,遍历是指到达这个点,经过它并不算),点与点之间的距离是两点坐标的绝对值,一条路径的长度等于根据某个顺序遍历完所有点所走的距离。求出所有路径的长度之和,然后根据这个和求出总的平均长度,并用最简分数表示。
假设有n个点,那么n个点的全排列是n!,然后先忽略点之间的距离(只考虑它们的先后顺序),考虑某2个点在这n!种排列中相邻出现的次数,那就是(n-1)!*2次(考虑上这两个点的顺序),然后考虑上距离那么这n个点相互到达的总距离就是Σ(|ai-aj|*2*(n-1)!),但是,还需要考虑以不同点作为第一个遍历的点的时候从0到达这个点所走的距离这和,对于某一个点一共有(n-1)!种情况是它作为开头第一个点,那走过的距离就是ai*(n-1)!这么多种,n个点就是Σai*(n-1)!。
所以总的所走距离就是Sum = Σ(|ai-aj|*2*(n-1)!)+Σ(ai*(n-1)!),AVG = Sum/n!约分以后得到: (Σ|ai-aj|*2+Σai)/n 由于n最大可以达到100000 所以时间复杂度要小于O(n^2),分析如下:

这是枚举不同的两个点的连线情况(上面的数字是下标),然后发现规律s'=s-2*i+1+n,s'是当前这个位置到还没有连线的位置的连线数目,s是上一个位置的连线数目。因为这是不考虑两个点的顺序的,所以对s'求和以后还要乘以2,再加上Σai就等于分子,然后分子和分母n都除以gcd(分子,分母)就得到正确答案了。 上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100010
#define LL long long
using namespace std; LL a[MAX];
LL sum,s,S; bool cmp(LL u,LL v){
return u<v;
} LL gcd(LL e,LL f){
return f== ? e : gcd(f,e%f);
} int main()
{
int n;
//freopen("data.txt","r",stdin);
scanf("%d",&n);
sum=;
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
S+=a[i];
}
//cout<<S<<endl;
sort(a+,a+n+,cmp);
s=;
for(int i=;i<n;i++){
s=s-*i++n;
sum+=s*(a[i+]-a[i]);
}
//cout<<sum<<endl;
sum<<=;
S+=sum;
//cout<<S<<endl;
LL g = gcd(S,n);
//cout<<g<<endl;
printf("%I64d %I64d\n",S/g,n/g);
return ;
}
340C
CodeForces - 340 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 ...
- C. Tourist Problem
http://codeforces.com/problemset/problem/340/C 赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容 ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- Codeforces 442B Andrey and Problem(贪婪)
题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...
- CodeForces 867B Save the problem
B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...
- Codeforces 776D The Door Problem
题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...
随机推荐
- 学界| UC Berkeley提出新型分布式框架Ray:实时动态学习的开端—— AI 应用的系统需求:支持(a)异质、并行计算,(b)动态任务图,(c)高吞吐量和低延迟的调度,以及(d)透明的容错性。
学界| UC Berkeley提出新型分布式框架Ray:实时动态学习的开端 from:https://baijia.baidu.com/s?id=1587367874517247282&wfr ...
- nyoj--990--蚂蚁感冒(模拟)(思维题)
蚂蚁感冒 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/ ...
- K-means (PRML) in C++
原始数据 #include <iostream>#include <fstream>#include <sstream>#include <vector> ...
- A - Supermarket
Problem description We often go to supermarkets to buy some fruits or vegetables, and on the tag the ...
- 利用 html+css 画同心圆(concentric circles)——绝对布局与相对布局
一.css 绘制圆 #circle { width: 300px; height: 300px; background-color: #000000; border-radius: 300px; } ...
- 用Python+selenium打开IE浏览器和Chrome浏览器的问题
这几天在学Python+selenium自动化,对三大浏览器Firefox,Chrome和IE都做了尝试,也都分别下载了对应的webdriver,如:geckodriver.chromedriver. ...
- (转)C#开发微信门户及应用(1)--开始使用微信接口
http://www.cnblogs.com/wuhuacong/p/3613826.html 微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习 ...
- dispatch_sync
dispatch_sync does two things: queue a block blocks the current thread until the block has finished ...
- 与Java注释相关的一些知识
* Html标签: * <a> 可定义锚,主要有以下两种属性 * href(最重要):创建指向另外一个文档的链接(或超链接) * ...
- 团体程序设计天梯赛-练习集-*L1-043. 阅览室
L1-043. 阅览室 天梯图书阅览室请你编写一个简单的图书借阅统计程序.当读者借书时,管理员输入书号并按下S键,程序开始计时:当读者还书时,管理员输入书号并按下E键,程序结束计时.书号为不超过100 ...