zoukankan      html  css  js  c++  java
  • 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 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  =  = .

    我们可以找出规律,答案就是(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
    ",(s+sum)/k,n/k);
        }
        return 0;
    }
    


  • 相关阅读:
    jmeter_04_常用取样器
    jmeter_03_鉴权
    jmeter_02_目录文档说明
    jmeter_01_常用快捷键
    Web Api 与 Andriod 接口对接开发经验
    Eclipse自动生成作者、日期注释等功能设置
    c#解析XML到DATASET及dataset转为xml文件函数
    Jquery 仿 android Toast效果
    正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
    异步网络加载开源框架AsyncHttpClient使用
  • 原文地址:https://www.cnblogs.com/pangblog/p/3320134.html
Copyright © 2011-2022 走看看