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.
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.
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.
4 1 2 3 12
3 1 2 12
4 1 1 2 2
3 1 1 2
2 1 2
2 1 2
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.
题意:给你n个数,让你从里面任选若干个数,使得这些数的平均值 - 中位数 最大。
思路:枚举中位数,二分左右区间长度。最优方案肯定是让平均值尽量大,那在右区间就选最大的那几个数,在左区间选靠近中位数的那几个数。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF (2000000000+10)
#define eps 1e-8
#define MAXN (200000+10)
#define MAXM (600000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
int a[MAXN];
LL sum[MAXN];
int main()
{
int n; Ri(n);
for(int i = 1; i <= n; i++) Ri(a[i]);
sort(a+1, a+n+1);
for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + a[i];
double ans = -1000000; int pos = 1, len = 0;
for(int i = 1; i <= n; i++)
{
int l = 1, r = min(i-1, n-i), res = 0;
while(r >= l)
{
int mid = (l + r) >> 1;
double res1 = 1.0*(sum[n] - sum[n-mid] + sum[i] - sum[i-mid-1]) / (2*mid+1);
double res2 = 1.0*(sum[n] - sum[n-mid+1] + sum[i] - sum[i-mid]) / (2*mid-1);
if(res1 > res2)
{
res = mid;
l = mid+1;
}
else
r = mid-1;
}
double ans1 = 1.0*(sum[n] - sum[n-res] + sum[i] - sum[i-res-1]) / (2*res+1) - a[i];
//printf("%.3lf %.3lf\n", ans1, ans);
if(ans1 > ans)
{
len = res;
ans = ans1;
pos = i;
}
}
Pi(len*2+1);
for(int i = pos-len; i < pos; i++) printf("%d ", a[i]);
for(int i = n-len+1; i <= n; i++) printf("%d ", a[i]);
Pi(a[pos]);
return 0;
}