Codeforces Round #198 (Div. 2) C. Tourist Problem
1 second
256 megabytes
standard input
standard output
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.
3
2 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 =
=
.
我们可以找出规律,答案就是(sum{pri[i]}+任意两点间的距离)/n,而任意两点间的距离我们不能用n*n来枚举,我们可以找出公式sum{(p[i]-p[i-1])*i*(n-i)*2};这样,排完序之后,就可以用线性时间内a掉了!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define M 100500
__int64 pri[M];
__int64 gcd(__int64 a,__int64 b){
if(a==0)return b;
return gcd(b%a,a);
}
bool cmp(int a,int b){
return a<b;
}
int main()
{
__int64 n,sum,s,k,tempn;
int i,j;
while(scanf("%I64d",&n)!=EOF){
for(sum=0,s=0,i=0;i<n;i++){
scanf("%I64d",&pri[i]);
sum+=pri[i];
}
sort(pri,pri+n,cmp);
for(s=0,i=1;i<n;i++){
s+=(pri[i]-pri[i-1])*i*(n-i);
}
s*=2;
k=gcd(s+sum,n);
printf("%I64d %I64d\n",(s+sum)/k,n/k);
}
return 0;
}
Codeforces Round #198 (Div. 2) C. Tourist Problem的更多相关文章
- 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 ...
- Codeforces Round #367 (Div. 2) C. Hard problem
题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...
- Codeforces Round #198 (Div. 2)A,B题解
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
- Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题
Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #198 (Div. 2)
A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...
- Codeforces Round #198 (Div. 2) 340C
C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #198 (Div. 2)C,D题解
接着是C,D的题解 C. Tourist Problem Iahub is a big fan of tourists. He wants to become a tourist himself, s ...
- Codeforces Round #198 (Div. 1 + Div. 2)
A. The Wall 求下gcd即可. B. Maximal Area Quadrilateral 枚举对角线,根据叉积判断顺.逆时针方向构成的最大面积. 由于点坐标绝对值不超过1000,用int比 ...
随机推荐
- POJ 2695 The Pilots Brothers' refrigerator(神奇的规律)
转载请注明出处:http://blog.csdn.net/a1dark 分析:如果想要将一个“+”翻转成“-”,那么必然会把对应的行和列上的所有点翻转一次.由于一个点翻偶数次就相当于不翻转.所以我需要 ...
- nodejs hello world (调用文件执行没反应)
今天学习了node.js ,来看一下hello world 的写法. 1. 首先安装node.js的驱动文件.http://nodejs.org/ 2. a:安装好node之后,在CMD窗口输入nod ...
- C# Code Snip
1.Tryf + TAB+TAB try { } finally { } 2.Prop+Tab+Tab public int MyProperty { get; set; } 3. #region + ...
- ajax的简单操作
项目需要,简单研究了下ajax 需要在html中引入js文件 编写js函数 function testAjax() { $.ajax({ type: 'get', //请求方式 get/post ur ...
- Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 1 -使用FOR XML返回XML结果集
XML 介绍 <CustomersOrders> <Customer custid="1" companyname="Customer NRZBB&qu ...
- html 7大知识点
HTML是web前端开发的基础,学习前端的人都是先从html学起的. 关于HTML有一些必备的知识点,这些知识点都是HTML中最基本的内容,也是前端面试最常问的知识点. 1.网页结构网页结构一般都包含 ...
- BZOJ 2882: 工艺( 后缀自动机 )
把串S复制成SS然后扔进后缀自动机里, 从根选最小的儿子走, 走N步就是答案了...一开始还想写个treap的...后来觉得太麻烦..就用map了... ----------------------- ...
- django 基础入门(二)
一.关于数据库 1.首先django 1.9以上等版本不支持pymysql,因此需要做一些调整. 比如在settings.py 加入一段代码: import pymysql pymysql.insta ...
- java线程中的sleep/wait/notify/yield/interrupt方法 整理
java线程中的sleep/wait/notify/yield/interrupt方法 sleep 该方法能够使当前线程休眠一段时间 休眠期间,不释放锁 休眠时间结束之后,进入可执行状态,加入到线程就 ...
- matrix67:kmp算法详解
个人认为KMP是最没有必要讲的东西,因为这个东西网上能找到很多资料.但网上的讲法基本上都涉及到“移动(shift)”.“Next函数”等概念,这非常容易产生误解(至少一年半前我看这些资料学习KMP时就 ...