Codeforces626E Simple Skewness(二分)

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值