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. 十五:SQL注入之oracle,Mangodb注入

    Access,Mysql,mssql,mangoDB,postgresql,sqlite,oracle,sybase JSON类型的数据注入: 键名:键值 {"a":"1 ...

  2. 【TNS】listener.ora模板;tnsnames.ora模板

    好多人使用监听的时候误操作,将监听弄的不好使了,这次这个模板,不光是针对大家出现的这种问题,也是给我自己留一个记录,方便他人,方便自己. listener.ora模板样例 -------------- ...

  3. 【网络】trunk和vlan配置

    篇一 : trunk配置和vlan配置 trunk配置 Switch>enable ? ? ?//进入特权模式 Switch#conf t ? ? ?//进入配置模式 Switch(config ...

  4. Linux下Too many open files问题排查与解决

    作者: Grey 原文地址: Github 语雀 博客园 Too many open files是Linux系统中常见的错误,从字面意思上看就是说程序打开的文件数过多,不过这里的files不单是文件的 ...

  5. LRU(Least Recently Used)最近未使用置换算法--c实现

    在OS中,一些程序的大小超过内存的大小(比如好几十G的游戏要在16G的内存上跑),便产生了虚拟内存的概念 我们通过给每个进程适当的物理块(内存),只让经常被调用的页面常驻在物理块上,不常用的页面就放在 ...

  6. Vue MVVM模型原理

    最近反思了下自己,觉得自己很急躁,学技术总是觉得能用就行了,其实这样很不好,总是这样,就永远只能当用轮子的人.好了,废话不多说,转入正题: 要理解MVVM的原理,首先要理解它是什么,怎么运作起来的: ...

  7. 封装JSONP 函数,方便请求发送

    封装JSONP 函数,方便请求发送 封装jsonp的代码和封装Ajax的代码非常的相似!可以参照食用偶! <button id="btn">点击我发送请求!</b ...

  8. libuv工作队列

    目录 1.说明 2.API 2.1.uv_queue_work 2.2.uv_cancel 3.代码示例 1.说明 libuv 提供了一个线程池,可用于运行用户代码,libuv 中的工作队列中的任务会 ...

  9. NoClassDefFoundError: javax/xml/bind/DatatypeConverter错误原因以及解决办法

    nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter 报错内容: org.sprin ...

  10. 基于final shell的linux命令

    final shell操作教程: 1.查看API实时日志:cd ../../data/log/api tail -100f anyAPI-server.log2.关闭日志:control+c3.恢复实 ...