Codeforces 626E Simple Skewness 【二分】

给定一组整数,任务是从中选择一个非空子集,使该子集的平均值减中位数达到最大。通过枚举中位数并使用二分查找优化区间长度,实现解决方案。

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

E. Simple Skewness
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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



题意:给你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;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值