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}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...
随机推荐
- 浅谈EL与JSTL
讲道理,自己在博大精深的Java世界里还只是一个很小很小的菜鸟.处于成长与学习之中,但学习一个漫长的过程.尤其对于那些知识点我觉得总结是尤为重要的.反正在我看来这段时间里虽然过了很多知识但却是一脸懵逼 ...
- inux内核模块编程入门
linux内核模块编程入门 2013-07-06 23:59:54 分类: LINUX 原文地址:linux内核模块编程入门 作者:s270768095 模块编程属于内核编程,因此,除了对内核相关知识 ...
- Android+Jquery Mobile学习系列(5)-SQLite数据库
SQLite是轻量级的.嵌入式的.关系型数据库,目前已经在iPhone.Android等手机系统中使用,SQLite可移植性好,很容易使用,很小,高效而且可靠. 因为Android已经集成了SQLit ...
- 【NOI 2014】 动物园
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3670 [算法] KMP [代码] #include<bits/stdc++.h ...
- Check the difficulty of problems(概率+DP)
http://poj.org/problem?id=2151 看的题解..表示没看懂状态转移方程.. #include<stdio.h> #include<string.h> ...
- JavaScript中变速运动的数学模型构建
AB两地直线距离相距为S,机器人β从A点向B点行进.已知机器人β的每间隔固定时间行进一段路程,其下次行进的距离为当前距离B点路程的1/q(q为正整数),求机器人第n次行进距离的表达式an以及前n项和公 ...
- springboot启动报错:Cannot determine embedded database driver class for database type NONE.
package cn.zb.test; import org.springframework.boot.SpringApplication; import org.springframework.bo ...
- PHP CURL抓取网页 simple_html_dom类
抓取网页数据后 数据录入到discuz中 <?php include('simple_html_dom.php'); function urlText(){ $url = 'http://www ...
- xhtml1-frameset.dtd
<!-- Extensible HTML version 1.0 Frameset DTD This is the same as HTML 4 Frameset except for chan ...
- 9 在C#控制台程序(console)中让用户输入
经过前面那些练习,我们已经熟悉录入一些简单的代码.这些代码可以进行一些简单的运算,在dos窗口打印出一些东西出来.我们现在要开始学习如何把数据从外部输入到我们的程序中. 其实大多数程序的工作是完成下面 ...