【codeforces 810C】Do you want a date?
【题目链接】:http://codeforces.com/contest/810/problem/C
【题意】
给你一个集合,它包含a[1],a[2]..a[n]这n个整数
让你求出这个集合的所有子集a的f(a)和
这里的f(a)=集合a中最大和最小元素的差;
【题解】
朴素的想法是;
将数组a升序排;
枚举最后的集合中最大和最小的元素是a[i]和a[j](i< j);
则f值是a[j]-a[i]的集合有2^(j-i-1)个;
即a[i+1]..a[j-1]这些元素选和不选….
然后答案+=2(j−i−1)∗(a[j]−a[i]);
但是这样枚举要O(N2)的复杂度;
我们可以做的更好;
一段一段地考虑;
比如考虑系数为
2^1的
必然是
a[3]-a[1]
a[4]-a[2]
a[5]-a[3]
…
a[n]-a[n-2]
=sum[n]-sum[1+1]-sum[n-i-1];
这里sum[i]是a[i]的前缀和
所以考虑枚举2^i的i;
然后O(1)获取系数为2^i的答案;
然后递增到答案里面;
sum[n]-sum[1+1]-sum[n-i-1]这里因为涉及到了取余,
所以可能会为负数,则+mod再%mod
【Number Of WA】
2
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 3e5;
const LL MOD = 1e9+7;
LL two[N+100],sum[N+100];
LL a[N+100];
int n;
int main()
{
//Open();
Close();//scanf,puts,printf not use
//init??????
two[0] = 1;
rep1(i,1,N)
two[i] = (two[i-1]*2)%MOD;
LL ans = 0;
cin >> n;
rep1(i,1,n)
cin >> a[i];
sort(a+1,a+1+n);
rep1(i,1,n)
sum[i] = (sum[i-1]+a[i])%MOD;
rep1(i,0,n-2)
{
LL temp = sum[n]-sum[i+1]-sum[n-i-1];
temp%=MOD;
if (temp<0)
temp = (temp+MOD)%MOD;
ans = (ans+two[i]*temp%MOD)%MOD;
}
cout << ans << endl;
return 0;
}
【codeforces 810C】Do you want a date?的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- Android 经常使用设计模式(一)
由于项目变更的频繁性,作为一名程序猿,我们须要掌握设计模式的必要性.就不言而喻~~.以下就是一些我自己学习的设计模式总结. 接下来,主要是针对几个比較经常使用模式进行解说,主要是以下几种: 观察者模式 ...
- 【Android 应用开发】 ActionBar 样式具体解释 -- 样式 主题 简单介绍 Actionbar 的 icon logo 标题 菜单样式改动
作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...
- MySQL具体解释(8)----------MySQL线程池总结(二)
这篇文章是对上篇文章的一个补充,主要环绕下面两点展开.one-connection-per-thread的实现方式以及线程池中epoll的使用. one-connection-per-thread 依 ...
- $scope angular在controller之外调用
1.定义 var m = angular.module('ddd',[]); m.controller('ctrl',['$scope',function ($scope) { }]); 2.外部调用 ...
- 杂项-DB-分布式:HBase
ylbtech-杂项-DB-分布式:HBase HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系 ...
- linux执行shell脚本时提示bad interpreter:No such file or directory的解决办法
故障现象:在终端直接cd /var正常,在shell脚本中执行则报错.原因是脚本是在windows平台下写的,换行符与Linux不同,造成脚本不能正确执行 出现bad interpreter:No s ...
- C#对 Json的序列化和反序列化时出现“k_BackingField”
在C#2.0的项目中,以前经常使用Json.NET实现序列化和反序列化.后来从c#3.0中开始使用新增的DataContractJsonSerializer进行json相关的操作.微软提供的原生类库使 ...
- HDU 1575 矩阵快速幂裸题
题意:中文题 我就不说了吧,... 思路:矩阵快速幂 // by SiriusRen #include <cstdio> #include <cstring> using na ...
- java实现简单回文算法
算法要求 编写一个程序,判断一个字符串是否为"回文".回文串:字符串字符从前往后与从后往前一致(中心对称). 算法思路 首先将字符串等分左右两块,然后依次对称比较每一对字符是否相同 ...
- javascript动画函数封装(升级版)
//把 任意对象 的 任意数值属性 改变为 任意的目标值 function animate(obj, json, fn) { clearInterval(obj.timer); obj.timer = ...