• 枚举 + 三分


    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 , 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.

    题目分析 : mean 平均值

    从 n 个数中选出任意个,让其平均值减去中位数答案最大,首先答案一定是奇数个,反证法,若你是偶数个数,当你去掉正中间稍大的那个后,所求答案一定是大于之前的情况的,

     

    那么根据这个条件我们就可以枚举中位数,然后再三分长度即可,因为当长度是 0 的时候,答案也为 0 ,当长度增加时,答案也在增加,在往后增加时,答案会变小,所以一定存在一个极致,三分即可

    代码示例 :

    #define ll long long
    const ll maxn = 2e5+5;
    const double pi = acos(-1.0);
    const ll inf = 0x3f3f3f3f;
    
    ll n;
    ll pre[maxn], sum[maxn];
    ll pos = 1, len = 0;
    double ans = 0;
    
    double fun(ll lenn, ll x){
        ll s = 0;
        s += sum[x] - sum[x-lenn-1];
        s += sum[n] - sum[n-lenn];
        double mean = 1.0*s/(1+lenn*2);
        return mean-1.0*pre[x];
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        cin >> n;
        for(ll i = 1; i <= n; i++){
            scanf("%lld", &pre[i]); 
        }
        sort(pre+1, pre+1+n);
        for(ll i = 1; i <= n; i++) sum[i] = sum[i-1] + pre[i];
        for(ll i = 2; i < n; i++){
            ll l = 1, r = min(i-1, n-i);
            for(ll j = 1; j <= 50; j++) {  
                ll lm = l + (r - l)/3;
                ll rm = r - (r - l)/3;
                double lf = fun(lm, i);
                double rf = fun(rm, i);
                
                if (lf > rf) r = rm;
                else l = lm;
                if (lf > ans) {pos = i; len = lm; ans = lf;}
                if (rf > ans) {pos = i; len = rm; ans = rf;}
            }
        }
        printf("%lld
    ", len*2+1);
        for(ll i = pos-len; i <= pos; i++){
            printf("%lld ", pre[i]);
        }   
        for(ll i = n-len+1; i <= n; i++){
            printf("%lld%c", pre[i], i==n?'
    ':' ');
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    对象关系一对多转换为一对一的方案——中介者模式总结
    接口转换的利器——适配器模式总结
    多线程场景设计利器:分离方法的调用和执行——命令模式总结
    对比总结三个工厂模式(简单工厂,工厂方法,抽象工厂)
    创建多个“产品”的方式——工厂方法模式总结
    Java反射+简单工厂模式总结
    最简单的设计模式——单例模式的演进和推荐写法(Java 版)
    对复合(协作)算法/策略的封装方法——装饰模式总结
    Java对象序列化全面总结
    创建产品族的方式——抽象工厂模式
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8457846.html
Copyright © 2020-2023  润新知