【codechef】Set Difference (灵活题,超时)
来源:互联网 发布:程序员应该读的书籍 编辑:程序博客网 时间:2024/05/19 01:08
Churu is working as a data scientist in Coderpur. He works on a lot of data on the daily basis. One day, he found an interesting problem, which was very easy to solve for small data but was getting more complex with increasing number of data points. So, Churu needs your help in solving this problem.
Given a set S of N non-negative integers (Some integers might occur more than once in the set), find out the value of SETDIFF(S).
Where max(s) represents the maximum value in set s whereas min(s) represents the minimum value in the set s.
As value of SETDIFF(S) can be very large, print it modulo (109 + 7) .
There might be repeated values in the set. For set S = {1,2,2}, consider that first 2 is not same as the second 2 and there will be two different subsets {1,2}. See last sample case for the more clarifications.
Input
- First line of input contains an integer T denoting number of test cases.
- For each test case, first line will contain an integer N denoting number of elements in set S.
- Next line contains N space separated integers denoting the set S.
Output
For each test case, print a single integer representing the answer of that test case.
Note
Two subsets will be called different if there exists an index i such that S[i] occurs in one of the subset and not in another.
Constraints
Subtask #1: 20 points
- 1 ≤ T ≤ 5, 1 ≤ N ≤ 1000, 0 ≤ value in set ≤ 109
Subtask #2: 25 points
- 1 ≤ T ≤ 5, 1 ≤ N ≤ 105, 0 ≤ value in set ≤ 1000
Subtask #3: 55 points
- 1 ≤ T ≤ 5, 1 ≤ N ≤ 105, 0 ≤ value in set ≤ 109
Example
Input:421 231 2 341 2 3 431 2 2Output:16 233
Explanation
For first case answer will be 2-1 = 1.
For the second case:
Subset = {1}, max(s)-min(s) = 0.
Subset = {2}, max(s)-min(s) = 0.
Subset = {3}, max(s)-min(s) = 0.
Subset = {1,2}, max(s)-min(s) = 1.
Subset = {2,3}, max(s)-min(s) = 1.
Subset = {1,3}, max(s)-min(s) = 2.
Subset = {1,2,3}, max(s)-min(s) = 2.
So the output will be 1+1+2+2 = 6.
In the last case, there are three subsets, {1,2}, {1,2} and {1,2,2} having max(s) - min(s) = 1 for each.
http://www.codechef.com/MAY15/problems/SETDIFF
找所有子集合的max-min的和的值
解法:排序,找出所有的i,j(i<j),sum+=(num[j]-num[j])*pow(j-i-1)。然而,在这里,我们能找出一个巧妙的结论:
式子分解,前半部分系数num[1]=2^0,num[2]=2^0+2^1,num[3]=2^0+2^1+2^2
后半部分系数:num[2]=2^0,num[1]=2^0+2^1,num[0]=2^0+2^1+2^2 =2^3-1。于是可以简化。
#include<iostream> #include<algorithm> #include<string> #include<map> #include<vector> #include<cmath> #include<queue> #include<string.h> #include<stdlib.h> #include<stdio.h> #define ll long long #define mod 1000000007 using namespace std; ll x[100001];int y[100001];int main() {y[0]=1;for(int i=1;i<100000;++i){y[i]=(y[i-1]*2)%mod;}int t;cin>>t;while(t--){int n;cin>>n;for(int i=0;i<n;++i)cin>>x[i];sort(x,x+n);ll s=0;for(int i=0;i<n;++i){ ll p=(x[i]%mod*((y[i]-1)-(y[n-1-i]-1))%mod)%mod;s=(s+p)%mod;}cout<<s<<endl;}return 0;}
这是我一开始的代码(20分):
#include<iostream> #include<algorithm> #include<string> #include<map> #include<vector> #include<cmath> #include<queue> #include<string.h> #include<stdlib.h> #include<stdio.h>#define ll long long #define mod 1000000007using namespace std;ll x[100001];ll y[100001];int main(){y[0]=1;for(int i=1;i<100000;++i)y[i]=(y[i-1]*2)%mod;int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);for(int i=0;i<n;++i)scanf("%I64d",&x[i]);sort(x,x+n);ll s=0;for(int i=0;i<n;++i){for(int j=i+1;j<n;++j){ll p=(x[j]-x[i])%mod;p=(p*y[j-i-1])%mod;s=(s+p)%mod;}}printf("%I64d\n",s);}return 0;}
- 【codechef】Set Difference (灵活题,超时)
- codechef(MAY15)第二题Set Difference
- codechef-Set Difference
- 【codechef】Gcd Queries(灵活题,分类)
- 【codechef】 Zeroes(大数灵活题)
- 【codechef】Arush Challenge(灵活题)
- 【codechef】Strings Classes(枚举灵活题)
- 【codechef】The Warehouse(灵活题)
- 【codechef】Chef and Strange Formula(找规律,灵活题)
- 【codechef】Chef and Polygons(灵活题,坑题)
- 【codechef】括号匹配,求子串最大值(灵活题)
- hdu 5247 找连续数(灵活,超时题)
- 【codechef】 Count Substrings (大数技巧题)(二分+计数,易超时)
- Codechef Maximum Weight Difference题解
- 【codechef】Feb.Challenge【逆元,超时题等】
- Set Difference(所有子集的最值差)
- CodeChef:L-R queries(线段树 & set)
- 【codechef】Magical Transformation(dp,技巧题)
- 常用命令oproflie
- 最小生成树【模板】
- bat - 通过adb批量安装apk脚本
- Android ViewPager内容部分随手势上下滑动隐藏与显示Indicator效果的实现
- HDU-2544单源最短路
- 【codechef】Set Difference (灵活题,超时)
- HDU2724 Tree【最小生成树】
- 简单介绍java Enumeration
- 【Mybatis】多参数查询与列表查询不同方式实现
- Matlab/Robotics Toolbox/添加到Matlab中使用
- mysql执行update提示ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction的应急解决方法
- Win7(x64) VS2013+Qt5.3.0+VTK6.1.0 安装测试
- 一个类中静态容器对象的初始化问题
- 可变参数列表函数实现