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}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...
随机推荐
- Yslow on Nodejs server
1. 目的:用yslow测试某个页面的性能 2. 需求:返回yslow测试后的数据,显示在页面 方法一. nodejs 需要把网址打包为har格式... 方法二. phantomjs 步骤: 1. 安 ...
- hdu 1242(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- poj--2031--Building a Space Station(prime)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6635 Accepte ...
- LocalDateTime查找最近的五分钟点
/** * 最近的五分钟 * @param dateTime * @return */ public static LocalDateTime getNear5(LocalDateTime dateT ...
- C# 标准命名规范
笔者从事开发多年,有这样一种感觉,查看一些开源项目,如Spring.Apache Common等源码是一件赏心悦目的事情,究其原因,无外两点:1)代码质量非常高:2)命名特别规范(这可能跟老外的英语水 ...
- 一个对象toString()方法如果没有被重写,那么默认调用它的父类Object的toString()方法,而Object的toString()方法是打印该对象的hashCode,一般hashCode就是此对象的内存地址
昨天因为要从JFrame控件获取密码,注意到一个问题,那就是用toString方法得到的不一定是你想要的,如下: jPasswordField是JFrame中的密码输入框,如果用下面的方法是得不到密码 ...
- 如何实现MySQL数据库使用情况的审计
如何实现MySQL数据库使用情况的审计 最佳答案 mysql的审计功能 mysql服务器自身没有提供审计功能,但是我们可以使用init-connect + binlog的方法进行mysql的操 ...
- JAVA 构建使用 Native 库
Java 使用Native文件,一般分解为下面几个步骤: 在Java代码中使用native关键字声明一个本地方法 运行javah,获得包含该方法声明的C语言头文件(使用jni编程中的C函数名通常是相关 ...
- DataGridView 单击赋值
void dataGridView1_Click(object sender, EventArgs e) { M_int_judge = ; btnSave.Enabled = true; btnSa ...
- RecyclerView 悬浮/粘性头部效果3种方式
但是以上两种方式onDrawOver()方法实现逻辑对初次查看该段代码要花时间理解.下面代码逻辑(原理一样,同样参考大神代码)相对清晰,易理解 public class StickyDecoratio ...