题目连接:
http://www.codeforces.com/contest/626/problem/E
Description
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.
Sample Input
4
1 2 3 12
Sample Output
3
1 2 12
Hint
题意
给你n个数,然后让你选出某些数出来,使得你选出来的数的平均值减去中位数最大
题解:
暴力枚举中位数,然后二分长度
显然我们知道中位数是什么,长度是什么之后,我们直接取最大的mid个数就好了
从n开始取mid个,从中位数取mid个,这样的平均值最大嘛。
我们可以大胆猜想(不用证明),长度的那个曲线是一个单峰的,所以我们三分或者二分去做,都兹瓷。
然后这道题就完了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
long long w[maxn],s[maxn];
int n;
long long get(int x,int i)
{
return s[x]-s[x-i-1]+s[n]-s[n-i];
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&w[i]);
sort(w+1,w+1+n);
for(int i=1;i<=n;i++)
s[i]+=w[i]+s[i-1];
int ans1=1,ans2=0;
double s1=0;
for(int i=2;i<=n;i++)
{
int l=2,r=min(n-i,i-1);
int tmp=1;
while(l<=r)
{
int mid=(l+r)/2;
if(get(i,mid-1)*(2*mid+1)<get(i,mid)*(2*mid-1))
{
tmp=mid;
l=mid+1;
}
else
r=mid-1;
}
double tmp2 = 1.0*get(i,tmp)/(1.0*2*tmp+1) - 1.0*w[i];
if(tmp2>s1)
{
s1=tmp2;
ans2=tmp,ans1=i;
}
}
printf("%d\n",ans2*2+1);
for(int i=ans1;i>ans1-ans2-1;i--)printf("%d ",w[i]);
for(int i=n;i>n-ans2;i--)printf("%d ",w[i]);
printf("\n");
}