Codeforces Round #415 (Div. 2) C. Do you want a date?

C. Do you want a date?

2 seconds

256 megabytes

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.

input

24 7

output

3

input

34 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.

题目大意:

     输入一集合S,定义F(a)函数(a为S的非空子集),F(a)=a集合中最大值与最小值的差

     计算所有F(a)的和。

解题思路:

     因为要求最大值与最小值的差,所以首先想到的是把S集合排序 (a[j]-a[i])*2^(j-i)

     对于只有一个元素的子集可以不用考虑

     

     对于上面的式子

     最小值一共要被减去2^n-2^0次 也可以理解为加上2^0-2^n次

     最大值一共被加上2^n-2^0次  第i个元素被加了2^i-2^(n-i-1)

AC代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <algorithm>
 7 const int mod = 1e9+7;
 8 int a[300010];
 9 __int64 b[300010];
10
11 using namespace std;
12
13 int main ()
14 {
15     int n,i,j;
16     while (~scanf("%d",&n))
17     {
18         b[1] = 1;
19         for (i = 1; i <= n; i ++)
20         {
21             cin>>a[i];
22             b[i+1] = b[i]*2%mod;   // 2^i  打表
23         }
24         sort(a+1,a+n+1);
25
26         __int64 sum = 0;
27         for (i = 1; i <= n; i ++)
28         {
29             sum += a[i]*(b[i]-b[n-i+1])%mod; // 直接代入公式
30             sum %= mod;
31         }
32         cout<<sum<<endl;
33     }
34     return 0;
35 }
36             scanf("%d%d%d",&l,&r,&x);
37             for(i = l; i <= r; i ++)
38                 if (p[i] < p[x])
39                     sum ++;
40             if (x-l == sum)
41                 printf("Yes\n");
42             else
43                 printf("No\n");
44         }
45     }
46     return 0;
47 }

     

时间: 2024-10-24 03:32:24

Codeforces Round #415 (Div. 2) C. Do you want a date?的相关文章

Codeforces Round #415 (Div. 2)

Straight <<A>> 暴力模拟一下. #include <iostream> #include <algorithm> #include <cmath> using namespace std; int main() { int n,k; cin>>n>>k; double sum = 0; for(int i=0;i<n;i++) { int x; cin>>x; sum += x; } dou

Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)

A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year.

Codeforces Round #415 (Div. 2) B. Summer sell-off(贪心+结构体排序)

题目链接:http://codeforces.com/contest/810/problem/B 题意:给定天数和货物可以翻倍的天数,每天都有一定的货物量和顾客数,问怎么样货物才能卖得最多(前一天的货物不会留到下一天,每个顾客只能买一个货物). 简单的贪心问题,贪心策略是:如果两倍的货物量卖出去的更多,就选两倍的,否则就选一倍的. 那一天能卖出去的货物量:min(货物量,顾客数).然后根据结构体排序一下就ok了 1 #include <iostream> 2 #include <algo

(二分)Codeforces Round #415 (Div. 2) D-Glad to see you!

This is an interactive problem. In the output section below you will see the information about flushing the output. On Sunday Leha the hacker took Nura from the house where she lives and went with her to one of the most luxurious restaurants in Vi?ko

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除