Simple Skewness CodeForces - 626E (三分)

该问题要求找到一个整数列表的非空子集,使其平均值与中位数之间的差值(简单偏度)最大化。解决方案涉及对列表排序,然后通过枚举中位数并使用三分法确定最佳子集。如果存在多个最优解,可以输出任意一个。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值