zoukankan      html  css  js  c++  java
  • 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 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
    ",u,v);
        }
        return 0;
    }





  • 相关阅读:
    基于 HTML5 + WebGL 实现的垃圾分类系统
    B/S 端基于 HTML5 + WebGL 的 VR 3D 机房数据中心可视化
    基于 Web 端 3D 地铁站可视化系统
    HTML5 + WebGL 实现的垃圾分类系统
    基于HTML5 WebGL的工业化3D电子围栏
    iOS 不支持 PWA,那又怎么样?
    PWA 入门: 写个非常简单的 PWA 页面
    iOS UTI
    canOpenURL: failed for URL: "weixin://app/wx 问题解决方式
    iOS扩大UIButton按钮的可点击区域
  • 原文地址:https://www.cnblogs.com/riskyer/p/3293862.html
Copyright © 2011-2022 走看看