C. Tourist Problem
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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 sequencea1a2, ..., 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 a1a2, ..., 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 test(s)
input
3
2 3 5
output
22 3
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  =  = .

思路:

1.分析第一步,有(0->(a1~an))共n中走法,每种走法会出现(n-1)!次。

2.分析其他步,ai->aj,先不考虑i、j两点,还有n-2各点,排列方式为(n-2)!种,n-2各点排列好后,就可以将i、j两点

看做一个整体插入到这个序列的中间(有n-1个位置可以插入),于是ai->aj的走法也会出现(n-1)!次。

所以推得公式为:[(a1+...+an)+∑|ai-aj|]/n,(i!=j) 。

ps:公式推出来只完成了一步,因为数据范围到了10^5。

3.以{a1,a2,a3,a4}为例,计算|ai-aj|实际上就是计算序列{a1,a2,a3,a4}任意两条线段的长度之和。

利用ai->aj覆盖了ai->a(j-1),从左向右观察,则以a2结束的线段只有S2=a1->a2,以a3结束的线段有a1->a3,a2->a3,

其中a1->a3可以看做a1->a2+a2->a3,这里a1->a2已经计算好了,所以S3=S2+2*(a2->a3)。S4同理。

4.若将数组a排好序,先只考虑i<j的情况(i>j的情况的值和i<j的情况值是一样的),就好处理了,就可以得到转移方

程s[i]=s[i-1]+(i-1)*|a[i]-a[i-1]|;s[i]为以i点为结束点的路径总和。

代码:

#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#define maxn 100005
using namespace std; typedef long long ll;
ll n,m,ans,u,v,sum;
ll a[maxn],s[maxn]; ll gcd(ll xx,ll yy)
{
ll r=xx%yy;
if(r==0) return yy;
else return gcd(yy,r);
}
void solve()
{
ll i,j,g;
u=0;
s[1]=0;
for(i=2;i<=n;i++)
{
s[i]=s[i-1]+(i-1)*fabs(a[i]*1.0-a[i-1]);
u+=s[i];
}
u=2*u+sum;
v=n;
g=gcd(u,v);
u/=g;
v/=g;
}
int main()
{
ll i,j;
while(~scanf("%I64d",&n))
{
sum=0;
for(i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
sort(a+1,a+n+1);
solve();
printf("%I64d %I64d\n",u,v);
}
return 0;
}

Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)的更多相关文章

  1. 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 ...

  2. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  3. Codeforces Round #603 (Div. 2) A. Sweet Problem(数学)

    链接: https://codeforces.com/contest/1263/problem/A 题意: You have three piles of candies: red, green an ...

  4. Codeforces Round #367 (Div. 2) C. Hard problem

    题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...

  5. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  6. 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 ...

  7. Codeforces Round #198 (Div. 2)

    A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...

  8. Codeforces Round #198 (Div. 2) 340C

    C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. 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 ...

随机推荐

  1. 旧版QT的名称:qt-win-commercial-4.4.3-vc60.exe

    qt-win-commercial-4.4.3-vc60.exeqt-vsaddin-collection-2.1.4.exeqt-win-commercial-4.4.3-v2005.exeqt-v ...

  2. 深度RAMOS,把操作系统全部安装在内存上

     你看下深度RAMOS就知道了  RAMOS+音速启动+绿色软件+云端  很爽 http://www.shenduwin7.com/jiaocheng/52.html

  3. 设置Oracle用IP远程连接和客户端访问

    要想将oracle设置为多用户可远程访问,须进行以下设置: 1.路径:D:\app\Administrator\product\11.1.0\db_1\NETWORK\ADMIN\listener.o ...

  4. 人事管理系统 c语言版

    int menu(){ printf("请按提示输入完毕操作!\n");   printf("1.查询员工信息\n");   printf("2.统计 ...

  5. mongodb查询分页优化

    要求不用skip 前提:1.时间倒序排列(自己现在的项目中也是按照时间倒序排列的)       2.每页显示10条数据 int limit = 10;//刚开始点击查询的时候设置十条 查询形式为 db ...

  6. wake_lock_timeout的用法

    今天实用到用ec43_GPIO的中断来唤醒系统,将系统从深度休眠中唤醒并保证系统wakup 一段时间用过了.方法例如以下.有相同使用的童鞋能够參考一下. 1.   定义一人局部静态变量ec43_wlo ...

  7. Web Api集成Swagger

    WebApi集成Swagger 1.新建一个WebApi空项目 2.新建一个Person实体类: public class Person { public int ID { get; set; } p ...

  8. 《转》在win7,boa-constructor 0.6.1 的palette面板中没有控件图标的解决方法

    原地址:http://blog.csdn.net/rickleo/article/details/6532595 在win7-64bit环境下,boa-constructor 0.6.1 的palet ...

  9. 深刻:截获windows的消息并分析实例(DefWindowProc),以WM_NCHITTEST举例(Windows下每一个鼠标消息都是由 WM_NCHITTEST 消息产生的,这个消息的参数包含了鼠标位置的信息)

    1,回调函数工作机制 回调函数由操作系统自动调用,回调函数的返回值当然也是返回给操作系统了. 2,截获操作系统发出的消息,截获到后,将另外一个消息返回给操作系统,已达到欺骗操作系统的目的. 下面还是以 ...

  10. [C++]引用浅析

    Date:2013-12-22 Summary: 引用数据类型的一些概念记录(沟通中提到引用必须结合语境才能知道说的是引用变量还是“引用”这一行为,再次提到引用指的一般是引用变量) Contents: ...