Codeforces Round #415 (Div. 2) C. Do you want a date?
2 seconds
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 allxi 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 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代码:
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- #include <iostream>
- #include <algorithm>
- const int mod = 1e9+;
- int a[];
- __int64 b[];
- using namespace std;
- int main ()
- {
- int n,i,j;
- while (~scanf("%d",&n))
- {
- b[] = ;
- for (i = ; i <= n; i ++)
- {
- cin>>a[i];
- b[i+] = b[i]*%mod; // 2^i 打表
- }
- sort(a+,a+n+);
- __int64 sum = ;
- for (i = ; i <= n; i ++)
- {
- sum += a[i]*(b[i]-b[n-i+])%mod; // 直接代入公式
- sum %= mod;
- }
- cout<<sum<<endl;
- }
- return ;
- }
- scanf("%d%d%d",&l,&r,&x);
- for(i = l; i <= r; i ++)
- if (p[i] < p[x])
- sum ++;
- if (x-l == sum)
- printf("Yes\n");
- else
- printf("No\n");
- }
- }
- return ;
- }
Codeforces Round #415 (Div. 2) C. Do you want a date?的更多相关文章
- Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- Codeforces Round#415 Div.2
A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...
- Codeforces Round #415(Div. 2)-810A.。。。 810B.。。。 810C.。。。不会
CodeForces - 810A A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes i ...
- Codeforces Round #415 Div. 1
A:考虑每对最大值最小值的贡献即可. #include<iostream> #include<cstdio> #include<cmath> #include< ...
- Codeforces Round #415 (Div. 2)C
反正又是一个半小时没做出来... 先排序,然后求和,第i个和第j个,f(a)=a[j]-a[i]=a[i]*(2^(j-i-1))因为从j到i之间有j-i-1个数(存在或者不存在有两种情况) 又有a[ ...
- Codeforces Round #415 (Div. 2) A B C 暴力 sort 规律
A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #415 (Div. 2) B. Summer sell-off
B. Summer sell-off time limit per test 1 second memory limit per test 256 megabytes Summer hol ...
- Codeforces Round #415 (Div. 2) 翻车啦
A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #415 (Div. 1) (CDE)
1. CF 809C Find a car 大意: 给定一个$1e9\times 1e9$的矩阵$a$, $a_{i,j}$为它正上方和正左方未出现过的最小数, 每个询问求一个矩形内的和. 可以发现$ ...
随机推荐
- oracle navicat 可视化操作进行数据的修改
在进行oracle数据库中的数据操作编辑时,需要小心.oracle内置的安全机制是无处不在,并且很有必要存在的. 使用navicat对oracle中数据进行select操作时,查询出的结果是只读的,这 ...
- 【Alpha】Scrum Meeting 0&1
前言 第0次会议和第1次会议分别在4月1日和4月2日21:00由PM在大运村一公寓3层召开. 第0次时长50min,主要明确了接下来的任务,对工作进行了分配. 第1次会议时长20min,调研了当日工作 ...
- [转] vagrant学习笔记 - provision
[From] https://blog.csdn.net/54powerman/article/details/50684844 从字面上来看,provision是准备,实现的功能是在原生镜像的基础 ...
- js计算数值
1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...
- vue中实现国际化--语言切换(转载)
https://segmentfault.com/a/1190000011800593
- mouseover和mouseenter闪烁的问题
span标签绑定mouseover/mouseout事件,显示/隐藏一个信息框div 该div下没有任何子元素 悬停上去一直闪烁,改成mouseenter也没用. 照成的原因是:悬停上去信息框div盖 ...
- Linux 时间日期类、搜索查找类、 压缩和解压类指令
l 时间日期类 date指令-显示当前日期 基本语法 1) date (功能描述:显示当前时间) 2) date +%Y (功能描述:显示当前年份) 3) date +%m (功能描述:显示当前月份) ...
- 学习java虚拟机笔记
虚拟机jvm 包括 类加载机制 ,运行时数据区域 运行时数据区域包括 ,程序计数器,虚拟机栈和本地方法栈,堆,方法区. 程序计数器是一块较小的内存控件, 用来指定当前字线程执行节码的行数 ,每个程序计 ...
- SQL数据库正在恢复 查看进度
在使用SQL的过程中.. 开启一个事务..进行大计算量..在中间出错或者强制杀死SQL服务进程..总之事务没有提交.. 再次开启时sql会进入自动检查的过程.. 数据库小的话问题不大..会比较快.. ...
- 判断title(title_is)
判断 title 获取页面 title 的方法可以直接用 driver.title 获取到,然后也可以把获取到的结果用做断言.本篇介绍另外一种方法去判断页面 title 是否与期望结果一种,用到上一篇 ...