E. Simple Skewness

time limit per test:3 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Define the simple skewness of a collection of numbers to be the collection's mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness.

The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of elements in the list.

The second line contains n integers xi (0 ≤ xi ≤ 1 000 000) — the ith element of the list.

Output

In the first line, print a single integer k — the size of the subset.

In the second line, print k integers — the elements of the subset in any order.

If there are multiple optimal subsets, print any.

Examples
Input
4
1 2 3 12
Output
3
1 2 12
Input
4
1 1 2 2
Output
3
1 1 2
Input
2
1 2
Output
2
1 2
Note

In the first case, the optimal subset is , which has mean 5, median 2, and simple skewness of 5 - 2 = 3.

In the second case, the optimal subset is . Note that repetition is allowed.

In the last case, any subset has the same median and mean, so all have simple skewness of 0.

题目链接:http://codeforces.com/contest/626/problem/E

题意

给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序)。

分析

因为是子集,所以不一定是连续的序列。然后我们有下面几个结论。

1.最大偏度一定≥0

因为一个元素时,偏度为0。

2.最大偏度子集必定有元素个数为奇数个的。

证:

如果当元素个数是偶数2*k时偏度最大,我们证明它去掉一个元素a[k+1]不会更差。

子集里排好序分别是a[i]。除去a[k+1]其它数的平均值为av

新平均值-旧平均值=av-(av+a[k+1])/2=(av-a[k+1])/2

新中位数-旧中位数=a[k]-(a[k]+a[k+1])/2=(a[k]-a[k+1])/2

且有 旧平均值-旧中位数=(av+a[k+1])/2-(a[k]+a[k+1])/2=(av-a[k])/2≥0 (否则不可能偏度最大)

所以有 平均值增量-中位数增量=(av-a[k])/2≥0

所以新的偏度肯定不会更差。

3.平均值先递增后递减

因为是奇数个,所以枚举每个数做中位数,假如左右延伸长度为j,那么要使偏度更大,我们一定是每次选剩下的里面左边最大和右边最大的数。所以剩下的数越来越小,平均值增加得越来越少,而当前平均值越来越大,到某个峰值后平均值就开始减小了。

所以可以用二分法每次取中点和中点旁边一个点判断当前平均值在增加还是减小,增加就往右找,减小就往左找。

顺便吐槽一句:cf测评机不行啊,跑这个跑了大半个小时才跑完!!!

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
int n;
ll s[N<<],a[N<<];
int ansi=,ansp;
double ans;
//最大偏度≥0,所以初始值就是只有第一个元素,偏度为0
ll sum(int i,int j)
{
return s[n]-s[n-j]+s[i]-s[i-j-];
}
int main()
{
n=read();
for(int i=;i<=n;i++)
cin>>a[i];
sort(a+,a++n);
for(int i=;i<=n;i++)
{
s[i]=s[i-]+a[i];
}
for(int i=;i<n;i++)
{
int l=,r=min(i-,n-i),m;
ll s1,s2;
while(l<r)
{
int mid=(l+r)/;
s1=sum(i,mid)*(*mid+);
s2=sum(i,mid+)*(*mid+);
if(s1>s2)
{
r=mid;
}
else
{
l=mid+;
if(s1==s2)
break;
}
}
if(1.0*sum(i,l)/(*l+)-a[i]>ans)
{
ans=1.0*sum(i,l)/(*l+)-a[i];
ansi=i;
ansp=l;
}
}
cout<<ansp*+<<endl;
cout<<a[ansi]<<" ";
for(int i=;i<=ansp;i++)
{
cout<<a[ansi-i]<<" "<<a[n-i+]<<" ";
}
cout<<endl;
return ;
}

最新文章

  1. “连接配置中心失败,将无法进行正常管理”--腾讯通RTX管理器
  2. PHP中想过获取系统内置的所有常量吗?
  3. 2016年省赛G题, Parenthesis
  4. 微信分享自定义图片标题摘要-微信官方API
  5. Hotel - poj 3667(求连续子区间)
  6. MySQL Update 使用
  7. Linux系统上的命令使用方法
  8. Document.write和 getElementById(ID)
  9. c++入门之 深入cin
  10. 001.Heartbeat简介
  11. OEMCC13.2 添加监控目标
  12. Executor(二)ThreadPoolExecutor、ScheduledThreadPoolExecutor 及 Executors 工厂类
  13. “吃神么,买神么”的第一个Sprint计划(第三天)
  14. Mina 断线重连
  15. JavaScript常见集合操作
  16. python,java操作mysql数据库,数据引擎设置为myisam时能够插入数据,转为innodb时无法插入数据
  17. iOS开发UIColor,CGColor,CIColor三者的区别和联系
  18. Delphi处理事件函数中的Sender: TObject代表什么?
  19. Mac开发
  20. Spring课程 Spring入门篇 4-7 Spring bean装配之基于java的容器注解说明--@Scope 控制bean的单例和多例

热门文章

  1. 下篇: php 微商城 基于Thinkphp3.2框架开发
  2. Qt---Javascript/Qt交互、脚本化
  3. 干货分享!关于APP导航菜单设计你应该了解的一切
  4. python中的virtualenv是干嘛的?
  5. [编织消息框架][JAVA核心技术]动态代理应用3
  6. springCloud系列教程01:Eureka 注册中心集群搭建
  7. kafka资料
  8. UWP 手绘视频创作工具技术分享系列 - 有 AI 的手绘视频
  9. and,or
  10. 【练习】jQuery