Codeforces626E Simple Skewness
codeforces题解:
We can show that any subset with maximal simple skewness should have odd size (otherwise we drop the larger middle element: this decreases the median by more than it decreases the mean, assuming the mean is larger than the median).
Let’s fix the median at xi (in the sorted list), and set the size of the set to 2j + 1. We’d like to maximize the mean, so we can greedily choose the largest j elements below the median and the largest j elements above the median: xi - j, ..., xi - 1 and xn - j + 1, ..., xn.
Now, notice that by increasing j by 1, we add in the elements xi - j - 1 and xn - j, which decrease as j increases. Thus, for a fixed i, the overall mean is bitonic in j (it increases then decreases), so we can binary search on the marginal utility to find the optimum.
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
using namespace std;
typedef long long LL;
typedef double DB;
typedef long double LDB;
#define INF 2147483647
#define mms(_x, _y) memset(_x, _y, sizeof(_x));
#define rep(_x, _y, _z) for(int _x = _y; _x <= _z; _x++)
#define per(_x, _y, _z) for(int _x = _y; _x >= _z; _x--)
#define BIGNUM int
const int MOD=1e9+7;
const double pi = acos(-1.0);
const double E = exp(1.0);
const int M = 2e5+9;
LL nu[M],su[M];
int l,r,mid,n,red=1,rel=0;
DB x1,x2,ma;
void Init(){
scanf("%d",&n);
rep(i,1,n)scanf("%I64d",&nu[i]);
}
void Solve(){
sort(nu+1,nu+n+1);
su[0]=0;
rep(i,1,n)su[i]=su[i-1]+nu[i];
ma=0;
rep(i,2,n-1){
l=1;
r=min(i-1,n-i);
while(l<r){
mid=l+r>>1;
x1=(DB(su[i]-su[i-mid-1]+su[n]-su[n-mid]))/(DB(mid*2+1));
x2=(DB(su[i]-su[i-mid-2]+su[n]-su[n-mid-1]))/(DB(mid*2+3));
if(x1>x2)r=mid;
else l=mid+1;
}
if(ma<(DB(su[i]-su[i-l-1]+su[n]-su[n-l]))/(DB(l*2+1))-nu[i]){
ma=(DB(su[i]-su[i-l-1]+su[n]-su[n-l]))/(DB(l*2+1))-nu[i];
red=i;
rel=l;
}
}
printf("%d\n%I64d",rel*2+1,nu[red]);
rep(i,1,rel)printf(" %I64d %I64d",nu[red-i],nu[n-i+1]);
}
int main(){
int _T = 1;
//scanf("%d",&_T);
while(_T--){
Init();
Solve();
}
return 0;
}