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.
Example
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 1,2,12 , which has mean 5, median 2, and simple skewness of 5 - 2 = 3.
In the second case, the optimal subset is 1,1,2 . Note that repetition is allowed.
In the last case, any subset has the same median and mean, so all have simple skewness of 0.
大致题意: 给你n个数,让你从中随便选一些数,使得这些数的平均值减去中位数最大。
思路:一定选择奇数个数。若选择偶数个数则可将中间两个数较大的一 个删除使得答案更优。所以我们先排个序,然后枚举中位数,三分区间长度。当中位数确定时,左边选的数肯定是越接近中位数,右边选的数肯定是越远离中位数,平均值才会越大。
代码如下
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
#define ll long long int
ll a[200005];
ll sum[200005];
int n;
int solve(int m1,int m2,int i)
{
ll s1=sum[n]-sum[n-m1]+sum[i]-sum[i-m1-1];
ll s2=sum[n]-sum[n-m2]+sum[i]-sum[i-m2-1];
return s1*(2*m2+1)<s2*(2*m1+1);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
if(n<=2)
{
printf("1\n");
printf("%I64d",a[1]);
return 0;
}
sort(a+1,a+n+1);
sum[0]=0;
for(int i=1;i<=n;i++)//预处理,后面在查询的时候能在o(1)的时间内知道某一段连续区间的和
sum[i]=sum[i-1]+a[i];
int wei1=1,wei2,len1=0,len2;
int l,r;
ll sum1=0,sum2;
for(int i=2;i<n;i++)//枚举中位数
{
l=1,r=min(n-i,i-1);//最多取的数量
int t=100;//三分次数
while(t--)
{
int m1=(2*l+r)/3;
int m2=(l+2*r+2)/3; //向上取整
if(solve(m1,m2,i))
l=m1+1;
else
r=m2-1;
}
sum2=sum[n]-sum[n-l]+sum[i]-sum[i-l-1]-(2*l+1)*a[i];
len2=l;
wei2=i;
if(sum2*(2*len1+1)>sum1*(2*len2+1))
{
sum1=sum2;
wei1=wei2;
len1=len2;
}
}
printf("%d\n",2*len1+1);
for(int j=wei1-len1;j<=wei1;j++)
printf("%I64d ",a[j]);
for(int j=n-len1+1;j<=n;j++)
printf("%I64d ",a[j]);
}