CodeForces 810C Do you want a date? 【数学】【排序】

来源:互联网 发布:军训教官 知乎 编辑:程序博客网 时间:2024/06/05 23:43

题目链接:Codeforces Round #415 (Div. 2) C

Descprition:
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 all xi 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 0 and 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.

题目大意:
告诉你一个含有n个互不相同的元素的集合,求它的所有非空子集中元素最大值与元素最小值之差的和。结果很大,输出它%1e9+7后的结果。

解题思路:
我们知道,一个非空集合共2^n个子集,而按照题意要求,集合中最大值减最小值大于0的集合共2^n-n-1个。
对满足题意的集合进行研究发现,对集合中的元素从小到大排序后,最终答案为∑(aj-ai)*2^(j-i-1)

Mycode:

#include <bits/stdc++.h>using namespace std;const int MAX = 3*1e5+7;const int MOD = 1e9+7;int a[MAX];long long b[MAX];int main(){    int n;    cin >> n;    b[1] = 1;    for(int i = 2; i <= n; ++i)    {        b[i] = b[i-1]<<1;        b[i] %= MOD;    }    for(int i = 0; i < n; ++i)    {        cin >> a[i];    }    sort(a, a+n);    long long ans = 0;    long long tem;    for(int i = 1; i < n; ++i)    {        for(int j = 0; j < i; ++j)        {            tem = (a[i] - a[j])*b[i-j]%MOD;            ans = (ans + tem) % MOD;        }    }    cout << ans << endl;    return 0;}

很遗憾,这样TLE了,于是重新思考,寻找新的方法。

对之前推出的式子,其中最小的元素被减了2^n-2^0次,即加了2^0-2^n次;最大的元素被加了2^n-2^0次……第i个元素被加了2^i-2^(n-i-1)。于是写出了下列代码,减少了一层循环,AC。
Mycode:

#include <bits/stdc++.h>using namespace std;const int MAX = 3*1e5 + 7;const int MOD = 1e9 + 7;int a[MAX];long long b[MAX];int main(){    int n;    cin >> n;    b[0] = 1;    for(int i = 0; i < n; ++i)    {        cin >> a[i];        b[i+1] = (b[i]*2) % MOD;    }    sort(a, a+n);    long long ans = 0;    for(int i = 0; i < n; ++i)    {        ans += (a[i]*(b[i]-b[n-i-1])) % MOD;        ans %= MOD;    }    cout << ans << endl;    return 0;}

OK,到这里问题就结束了,总结一下,很多时候一些思维的题不只是需要灵光一闪,其实平时的积累也是很重要的,做多了这种题,套路自然就形成了。

原创粉丝点击