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

题意:给你n个数,让你找到一个非空子集合,使得这个子集合的平均数和中位数的差最大。

思路:首先,这个产生最大值的子集合内含的数的个数一定是奇数(平均数不等于中位数),因为如果个数是偶数,那么我们可以减去中间较打的一个数,那么平均数减去中位数的值就会变大。我们可以枚举每一个数为中位数,然后三分找到最大的平均数。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 200050
ll a[maxn];
ll n,m;
ll sum[maxn]; int main()
{
ll i,j;
while(scanf("%lld",&n)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%I64d",&a[i]);
}
sort(a+1,a+1+n);
sum[0]=0;
for(i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i];
}
if(n==1 || n==2){
printf("1\n");
printf("%I64d\n",a[1]);continue;
}
ll l,r,d,m1,m2,t1,t2;
ll mid=1,len=0,zong=0;
ll zong1,mid1,len1;
for(i=2;i<=n-1;i++){
l=1;r=min(i-1,n-i);
for(j=1;j<=100;j++){
m1=(2*l+r)/3;
m2=(l+2*r+2)/3; //向上取整
/*
d=(l+r)/2;
m1=d;
m2=(d+r)/2;
*/
t1=sum[i]-sum[i-m1-1]+sum[n]-sum[n-m1];
t2=sum[i]-sum[i-m2-1]+sum[n]-sum[n-m2];
if(t1*(2*m2+1)<t2*(2*m1+1)){
l=m1+1;
}
else r=m2-1;
}
zong1=sum[i]-sum[i-l-1]+sum[n]-sum[n-l]-(2*l+1)*a[i];
len1=l;
mid1=i;
if(zong1*(2*len+1)>zong*(2*len1+1) ){
zong=zong1;
len=len1;
mid=mid1;
}
}
printf("%I64d\n",len*2+1);
int flag=1;
for(i=mid-len;i<=mid;i++){
if(flag){
flag=0;printf("%I64d",a[i]);
}
else{
printf(" %I64d",a[i]);
}
}
for(i=n-len+1;i<=n;i++){
printf(" %I64d",a[i]);
}
printf("\n");
}
return 0;
}

codeforces626E.Simple Skewness(三分)的更多相关文章

  1. codeforces 626E. Simple Skewness 三分

    题目链接 给n个数, 让你去掉一些数, 使得剩下的数的平均值-中位数的差值最大. 先将数组排序, 然后枚举每一个数作为中位数的情况, 对于每个枚举的数, 三分它的左右区间长度找到一个平均值最大的情况, ...

  2. 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness 暴力+二分

    E. Simple Skewness 题目连接: http://www.codeforces.com/contest/626/problem/E Description Define the simp ...

  3. Codeforces 626E Simple Skewness(暴力枚举+二分)

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

  4. 【CodeForces 626E】Simple Skewness

    题意 给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序). 分析 因为是子集,所以不一定是连续的序列.然后我们有下面几个结论. 1.最大偏度一 ...

  5. Codeforces 626E Simple Skewness 「数学」「二分」

    题意: 给你一堆无序数,寻找它的一个子堆,使得子堆的平均数减中位数最大. 数字的个数n<=2e5 0<=xi<=1e6. 思路: 首先可以证明这堆数一定是奇数个,证明方法是尝试在奇数 ...

  6. codeforces 练习

    codeforces 627 D. Preorder Test 二分 + 树dp 做logn次树dp codeforces 578D.LCS Again 给出一个字符串str,长度n<=10^6 ...

  7. 8VC Venture Cup 2016 - Elimination Round

    在家补补题   模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, in ...

  8. codeforce 626E(二分)

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

  9. PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)

    最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...

随机推荐

  1. LeetCode144 二叉树的前序遍历

    给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? /** * Defin ...

  2. 怎么启用apache的mod_log_sql模块将所有的访问信息直接记录在mysql中

    怎么启用apache的mod_log_sql模块将所有的访问信息直接记录在mysql中

  3. vmstat参数详解

    vmstat 5 可以使用ctrl+c停止vmstat,可以看到输出依赖于所用的操作系统,因此可能需要阅读一下手册来解读报告 第一行的值是显示子系统启动以来的平均值,第二行开始展示现在正在发生的情况, ...

  4. JDBC入门程序总结

    JDBC本质 只是一个接口 每个数据库的规范 就是实现类的接口 其实是官方 定义的一套操作所有关系型数据库的规则,就是接口,各个数据库厂商去实现这套接口,提供数据库驱动jar包, 我们可以使用这套接口 ...

  5. SwiftUI 官方画图实例详细解析

    前言 在前面几篇关于SwiftUI的文章中,我们用一个具体的基本项目Demo来学习了下SwiftUI,里面包含了常见的一些控件使用以及数据处理和地图等等,有兴趣的小伙伴可以去翻翻以前的文章,在前面总结 ...

  6. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  7. 我在华为OD的275天

    目录 0 - 时间线 1 - 为什么会去华为 OD 2 - 华为 OD 的工作内容 3 - OD 与华为自有员工的对比 4 - 那,到底要不要去华为 OD? 5 - 网传的 OD 转华为正编,真的假的 ...

  8. Hive常用性能优化方法实践全面总结

    Apache Hive作为处理大数据量的大数据领域数据建设核心工具,数据量往往不是影响Hive执行效率的核心因素,数据倾斜.job数分配的不合理.磁盘或网络I/O过高.MapReduce配置的不合理等 ...

  9. Navicat 创建mysql存过、定时执行存过

    创建存过: 使用Navicat for MySQL工具创建存储过程步骤: 1. 新建函数(选择函数标签 -> 点击新建函数): 2.输入函数的参数个数.参数名.参数类型等: 3.编写存储过程: ...

  10. SpringBoot深入理解

    SpringBoot深入理解 项目打包SpringBoot启动过程 当使用打包时,会下载org-springframework-boot-loader的jar,并且不会放在lib存放的第三方jar包文 ...