• codeforces 810C


    C. Do you want a date?
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.

    Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.

    Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.

    Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression . Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, . Since the required sum can be quite large Noora asks to find it modulo 109 + 7.

    Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.

    Input

    The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.

    The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that allxi are distinct.

    Output

    Print a single integer — the required sum modulo 109 + 7.

    Examples
    input
    2
    4 7
    output
    3
    input
    3
    4 3 1
    output
    9
    Note

    There are three non-empty subsets in the first sample test: and . The first and the second subset increase the sum by 0and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.

    There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: . In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.

    题意:给定一个序列F,让你求F的每个子集里的最大数-最小数的和(%1e9+7);

    分析:如果从小到大排序,然后暴力枚举上下界然后乘以pow(2,界内元素个数)求解的话时间复杂度是O(n²),因此不可取;枚举上下界会超时,换个思维,排序后,我们可以尝试对于乘以pow(2,i)的这些子集,我们可以求和,然后*pow(2,i);这类问题需要维护前缀和,推导得到的公式为sum[n]-sum[i+1]-sum[n-i-1];i从0到n-2;

    AC代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 long long a[300005];
     4 long long mod=1e9+7;
     5 long long qpow(long long x,long long n){
     6     if(n==0) return 1;
     7     long long ans=1;
     8     while(n!=0){
     9         if(n&1) ans=(ans%mod)*(x%mod)%mod;
    10         n=n/2;
    11         x=(x%mod)*(x%mod)%mod;
    12     }
    13     return ans;
    14 }
    15 long long sm[300005];
    16 int main(){
    17     int n;
    18     while(cin>>n){
    19         for(int i=1;i<=n;i++){
    20             cin>>a[i];
    21         }
    22         long long sum=0;
    23         sort(a+1,a+n+1);
    24         sm[1]=a[1];
    25         for(int i=2;i<=n;i++){
    26             sm[i]=a[i]+sm[i-1];
    27         }
    28         long long ans;
    29         for(int i=0;i<=n-2;i++){
    30             ans=(sm[n]-sm[i+1]-sm[n-i-1]+mod)%mod;
    31             sum=(sum+(qpow(2,i)*ans)%mod)%mod;
    32         }
    33         cout<<sum<<endl;
    34     }
    35 
    36 return 0;
    37 }
    View Code
  • 相关阅读:
    一种稀少的漏洞-内网穿透代理漏洞【原创】
    实战修改密码处的一处逻辑问题【原创】
    Redis Cluster集群搭建与配置
    tomcat热部署,更改java类不用重新加载context
    Redis集群方案应该怎么做
    Flume + HDFS + Hive日志收集系统
    hadoop 1.2 集群搭建与环境配置
    epub、ocf等常用电子书格式浅析----附JAVA示例程序
    zookeeper-3.4.8 集群搭建
    centos6.6 虚拟机集群搭建
  • 原文地址:https://www.cnblogs.com/ls961006/p/6906477.html
Copyright © 2020-2023  润新知