zoukankan      html  css  js  c++  java
  • codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

    Tourist Problem

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

    给出n个点的坐标,然后从0出发,可以以任何一个点为终点,问走过的路程的长度的期望

    这个的公式证明也是挺容易的。。。懒得写了。。。具体看代码吧。。。

     1 //#####################
     2 //Author:fraud
     3 //Blog: http://www.cnblogs.com/fraud/
     4 //#####################
     5 #include <iostream>
     6 #include <sstream>
     7 #include <ios>
     8 #include <iomanip>
     9 #include <functional>
    10 #include <algorithm>
    11 #include <vector>
    12 #include <string>
    13 #include <list>
    14 #include <queue>
    15 #include <deque>
    16 #include <stack>
    17 #include <set>
    18 #include <map>
    19 #include <cstdio>
    20 #include <cstdlib>
    21 #include <cmath>
    22 #include <cstring>
    23 #include <climits>
    24 #include <cctype>
    25 using namespace std;
    26 #define XINF INT_MAX
    27 #define INF 0x3FFFFFFF
    28 #define MP(X,Y) make_pair(X,Y)
    29 #define PB(X) push_back(X)
    30 #define REP(X,N) for(int X=0;X<N;X++)
    31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
    32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
    33 #define CLR(A,X) memset(A,X,sizeof(A))
    34 #define IT iterator
    35 typedef long long ll;
    36 typedef pair<int,int> PII;
    37 typedef vector<PII> VII;
    38 typedef vector<int> VI;
    39 
    40 #define MAXN 100010
    41 ll a[MAXN];
    42 int main()
    43 {
    44     ios::sync_with_stdio(false);
    45     ll n;
    46     while(cin>>n){
    47         for(int i=0;i<n;i++)cin>>a[i];
    48         sort(a,a+n);
    49         ll tx=0;
    50         ll ty=0,s=0;
    51         for(ll i=0,j=n-1;i<n;i++,j--){
    52             tx+=i*a[i];
    53             ty+=j*a[i];
    54             s+=a[i];
    55         }
    56         s+=(tx-ty)*2LL;
    57         ll tmp=__gcd(s,n);
    58         cout<<s/tmp<<" "<<n/tmp<<endl;
    59     }
    60             
    61     return 0;
    62 }
    代码君
  • 相关阅读:
    tableviewCell折叠状态1
    iOS中--NSArray调用方法详解 (李洪强)
    NSNumber的使用
    Fedora13下编译busybox-1.15.0出现can not find lcrypt错误
    【独立开发人员er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势
    JAVA序列化的作用
    我买网B轮融资成功,五周年豪掷千万回馈会员
    一步步教你搭建TinyOS2.1.2开发环境
    POJ2947 DAZE [Gauss]
    慢慢理解RESTful架构
  • 原文地址:https://www.cnblogs.com/fraud/p/4376959.html
Copyright © 2011-2022 走看看