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代码:
#include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define mod 1000000007
long long a[MAX+],b[MAX+],ans=;
int main()
{
int n,i;
scanf("%d",&n);
b[]=;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=(*b[i-])%mod;
}
sort(a+,a+n+);
for(i=;i<=n;i++)
{
ans+=(b[i-]-b[n-i])*a[i]%mod;
ans%=mod;
}
cout<<ans<<endl;
}
Codeforces 810C Do you want a date?(数学,前缀和)的更多相关文章
- 【组合 数学】codeforces C. Do you want a date?
codeforces.com/contest/810/problem/C [题意] 给定一个集合A,求 , 输入: [思路] 基数为n的集合有2^n-1个非空子集. 首先n个数要从小到大排序,枚举最后 ...
- 【codeforces 810C】Do you want a date?
[题目链接]:http://codeforces.com/contest/810/problem/C [题意] 给你一个集合,它包含a[1],a[2]..a[n]这n个整数 让你求出这个集合的所有子集 ...
- Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学
E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...
- Codeforces Round #207 (Div. 1)B(数学)
数学so奇妙.. 这题肯定会有一个循环节 就是最小公倍数 对于公倍数内的相同的数的判断 就要借助最大公约数了 想想可以想明白 #include <iostream> #include< ...
- codeforces 2B The least round way(DP+数学)
The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...
- Codeforces Round #499 (Div. 2) C. Fly(数学+思维模拟)
C. Fly time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- CodeForces - 586C Gennady the Dentist 模拟(数学建模的感觉)
http://codeforces.com/problemset/problem/586/C 题意:1~n个孩子排成一排看病.有这么一个模型:孩子听到前面的哭声自信心就会减弱:第i个孩子看病时会发出v ...
- Codeforces Beta Round #11 B. Jumping Jack 数学
B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...
- 【CodeForces】708 B. Recover the String 数学构造
[题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...
随机推荐
- iOS Label 自适应高度
推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirje ...
- Linux中dos2unix批量转换
有时候遇到多层目录下的文件格式需要转换,dos2unix 没有-r之类的递归指令,所以需要与find还有管道结合. find -type f | xargs dos2unix -o
- Python 项目实践三(Web应用程序)第一篇
一 Djangao入门 当今的网站实际上都是富应用程序(rich application),就像成熟的桌面应用程序一样.Python提供了一组开发Web应用程序的卓越工具.在本章中,你将学习如何使用D ...
- python中的virtualenv是干嘛的?
众所周知,python的各种库跨度比较大,比如如果你开发web的话,一个项目使用的Django是1.8, 而另一个项目使用的Django版本是1.7, 这就给开发人员带来了很大的困扰. 因此,pyth ...
- 解决openssh漏洞,升级openssh版本
关于解决漏洞的问题我就不详说了,主要就是升级版本.这里我们就直接简单记录下步骤: 1.升级 使用root用户登录系统进入到/home/guankong ,上传openssh-6.6p1.tar.gz到 ...
- python net-snmp 的使用
这一年一直在做一个综合管控平台的项目,用python写的,项目春节前可能就要进行实际部署了和测试,趁着这个空闲期,回顾一下项目中用到的一些技术,第一个就是SNMP协议. 项目结构主要是实现对ipran ...
- ExpandableListView的完美实现,JSON数据源,右边自定义图片
转载请标明出处: http://www.cnblogs.com/dingxiansen/p/8194669.html 本文出自:丁先森-博客园 最近在项目中要使用ExpandableListView来 ...
- 保存html上传文件过程中遇到的字节流和字符流问题总结
java字节流和字符流的区别以及相同 1. 字节流文件本身进行操作,字符流是通过缓存进行操作, 1.1 使用字节流不执行关闭操作 File f =new File("d:/test/test ...
- 一起学习Hibernate: Hibernate01 —— Hibernate的概述与入门案例
一 Hibernate的介绍 1 让我们从JDBC与替代它的框架Hibernate进行一下对比. 1.1 JDBC的缺点 1) 代码结构繁琐.每次书写sql语句操作数据库都得需要很多步; 2) 是面向 ...
- 适合小白/外行的git与github最基础最浅显教程
首先声明,这是适合小白/外行/初学者/学生看的最基础最简单的git与github教程,已经能使用svn,git等工具的朋友请不要看这篇文章来浪费时间了. 想进一步学习git的,推荐去廖雪峰博客学习. ...