Codeforces 810C Do you want a date?(数学,前缀和)
C. Do you want a date?
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.
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.
Print a single integer — the required sum modulo 109 + 7.
2
4 7
3
3
4 3 1
9
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.
题目链接:http://codeforces.com/contest/810/problem/C
分析:还是因为我太菜了,想了半天都没想出来怎么去写,上网查题解半天没有搞懂解题思路,过了一个小时灵光一现,答案出来了!
题意:给定集合,让我们求出所有子集中最大值最小值差的和
思路:很容易我们会想到直接排序,然后暴力枚举a[i]和a[j],那么他们之间的包含i位和j位的子集个数一定是2^(j-i-1)个,ans = 2^(j-i-1)*(a[j]-a[i])
但是一定会超时的啊,复杂度为O(n^2),所以我们考虑
长度为1结果是ans1 = a[1]-a[0]+a[2]-a[1] ....a[n-1]-a[n-2] = (a[1]+a[2]+...+a[n-1] )- (a[0]+a[1]+....+a[n-2])
长度为2是结果是ans2 = a[2]-a[0]+a[3]-a[1]+.....+a[n-1]-a[n-1-2]
这样我们可以清楚的发现ans = ans1+ans2+ans(n-1) = sum[n-1]-sum[i]-sum[n-1-i-1],这里sum[i]代表前i个的和
下面给出AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define MAX 300000 4 #define mod 1000000007 5 long long a[MAX+5],b[MAX+5],ans=0; 6 int main() 7 { 8 int n,i; 9 scanf("%d",&n); 10 b[0]=1; 11 for(i=1;i<=n;i++) 12 { 13 scanf("%d",&a[i]); 14 b[i]=(2*b[i-1])%mod; 15 } 16 sort(a+1,a+n+1); 17 for(i=1;i<=n;i++) 18 { 19 ans+=(b[i-1]-b[n-i])*a[i]%mod; 20 ans%=mod; 21 } 22 cout<<ans<<endl; 23 }
作 者:Angel_Kitty
出 处:https://www.cnblogs.com/ECJTUACM-873284962/
关于作者:阿里云ACE,目前主要研究方向是Web安全漏洞以及反序列化。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
欢迎大家关注我的微信公众号IT老实人(IThonest),如果您觉得文章对您有很大的帮助,您可以考虑赏博主一杯咖啡以资鼓励,您的肯定将是我最大的动力。thx.
我的公众号是IT老实人(IThonest),一个有故事的公众号,欢迎大家来这里讨论,共同进步,不断学习才能不断进步。扫下面的二维码或者收藏下面的二维码关注吧(长按下面的二维码图片、并选择识别图中的二维码),个人QQ和微信的二维码也已给出,扫描下面👇的二维码一起来讨论吧!!!
欢迎大家关注我的Github,一些文章的备份和平常做的一些项目会存放在这里。