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个数,让你从里面任选若干个数,使得这些数的平均值 - 中位数 最大。

分析:

先将原序列从小到大排序,再枚举中位数,求每一个中位数的最大平均数

子序列的长度一定是奇数,当个数由奇数个变成偶数个时,中位数增加的比平均数增加的要多,(平均数-中位数)的值就会变小

针对每一个中位数,二分总长度的长度的一半len,找到最大的平均数。

中位数是arr[i],长度的一半为len时,为了使平均数尽可能的大,找到的子序列一定是:arr[i-len]……arr[i] && arr[n-len+1]……arr[n](arr[]从1开始)

当len+1时,子序列中将增加arr[i-len-1]和arr[n-len],增加的数是减小的,所以随着len的增加,平均数总的来说应该是先增大再减小的,所以可以用二分求len

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int Max = + ;
LL arr[Max],sum[Max];
int main()
{
int n;
scanf("%d", &n);
sum[] = ;
for(int i = ; i <= n; i++)
{
scanf("%I64d", &arr[i]);
sum[i] = sum[i - ] + arr[i];
}
int res,pos = ,len = ;
double maxRes = -;
for(int i = ; i <= n; i++)
{
int l = , r = min(i - , n - i);
res = ;
while(r >= l)
{
int mid = (l + r) >> ;
double mean1 = 1.0 * (sum[i] - sum[i - mid - ] + sum[n] - sum[n - mid]) / ( * mid + );
double mean2 = 1.0 * (sum[i] - sum[i - mid] + sum[n] - sum[n - mid + ]) / ( * (mid - ) + );
if(mean1 > mean2) //与前一个进行比较,如果比前一个大左边就往前移,否则往右移,因为平均数是先变大后变小的通过与前面的比较找到最大的那个;
{
res = mid;
l = mid + ;
}
else
{
r = mid - ;
}
}
double ans = 1.0 * (sum[i] - sum[i - res - ] + sum[n] - sum[n - res]) / ( * res + ) - arr[i];
if(ans > maxRes)
{
maxRes = ans;
pos = i;
len = res;
}
}
printf("%d\n", * len + );
//题目对输出要求不严格,相同的结果随便输出
for(int i = pos - len; i < pos; i++)
printf("%I64d ", arr[i]);
for(int i = n - len + ; i <= n; i++)
printf("%I64d ", arr[i]);
printf("%I64d\n", arr[pos]); return ;
}

codeforce 626E(二分)的更多相关文章

  1. codeforce 359D 二分+ 动态规划(sparse table)

    原题链接:http://codeforces.com/problemset/problem/359/D 思路:首先对符合题目的长度(r-l)从0到n-1进行二分查找,对每一个长度进行check,看是否 ...

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

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

  3. codeforce 702C Cellular Network 二分答案

    http://codeforces.com/contest/702 题意:n个村庄,m个机站,问机站最短半径覆盖完所有村庄 思路:直接二分答案 二分太弱,调了半天..... // #pragma co ...

  4. codeforce 955c --Sad powers 思路+二分查找

    这一题的题意是   定义一个数,该数特点是为a的p次方 (a>0,p>1) 再给你n个询问,每个询问给出一个区间,求区间内该数的数目. 由于给出的询问数极大(10e5) 所以,容易想到应该 ...

  5. codeforce 1070 E Getting Deals Done(二分求最大化最小值)

    Polycarp has a lot of work to do. Recently he has learned a new time management rule: "if a tas ...

  6. codeforce Gym 100500F Door Lock (二分)

    根据题意略推一下,其实就是问你满足(a*(a+1))/2 < m <= ((a+1)*a(a+2))/2的a和m-(a*(a+1))/2 -1是多少. 二分求解就行了 #include&l ...

  7. [codeforce 975C] Valhalla Siege (二分)

    Examples input 5 5 1 2 1 2 1 3 10 1 1 1 output 3 5 4 4 3 input 4 4 1 2 3 4 9 1 10 6 output 1 4 4 1 N ...

  8. 二分题 D - Salary Changing codeforce

    题意:给出n个人(n是奇数),s钱:s为总的可以付工钱的钱: 每一个工人有一个付工钱的区间,只要在这个区间范围内,随便一个数都可以当作给这个工人付了钱: 老板要付给每个工人钱,并且付钱的中位数要尽可能 ...

  9. Codeforce 380A Sereja and Prefixes【二分】

    题意:定义两种操作 1 a ---- 向序列中插如一个元素a 2 a b ---- 将序列的前a个元素[e1,e2,...,ea]重复b次插入到序列中 经过一列操作后,为处于某个位置p的元素是多少.数 ...

随机推荐

  1. Netty解决TCP粘包/拆包问题 - 按行分隔字符串解码器

    服务端 package org.zln.netty.five.timer; import io.netty.bootstrap.ServerBootstrap; import io.netty.cha ...

  2. SqlDevlepor注册表监听器设置

      1.打开plsqldev.   2. 键入环境变量 NLS_LANG SIMPLIFIED CHINESE_CHINA.ZHS16GBK   3.下载sqldevclient. http://pa ...

  3. Objective-c复制对象的概念

  4. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  5. 《Linux内核设计与实现》课程学习重点问题总结

    (问题均是同学提出或是老师上课重点讲解的部分内容,根据自身理解和笔记总结出自己的答案.如有不对,还请指教.) week2 [Q1]命令qemu -kernel 内核可执行文件 -initrd root ...

  6. html 空格-有趣的试验

    首先,先给大家看一组demo <input /> <input type="submit" /> 展示效果: 为什么会出现空格呢?input不是行内元素吗? ...

  7. 慢牛APP相关截图

    慢牛APP相关截图 第一和第二个版本都是基于Sencha Touch+Cordova开发,公众号是采用Angularjs+D3开发,第三个版本是采用React Native开发. 第一个版本 第二个版 ...

  8. [电子书] 《Android编程入门很简单》

    <Android编程入门很简单>是一本与众不同的Android学习读物,是一本化繁为简,把抽象问题具体化,把复杂问题简单化的书.本书避免出现云山雾罩.晦涩难懂的讲解,代之以轻松活泼.由浅入 ...

  9. XP明年就被停止技术支持,这会带来什么?谈谈如何做决策

    XP是MS的一款老牌操作系统,相信大家都不陌生,甚至还有继续使用的人,当然了,在虚拟机里用它也是很好用的,不过,再漂亮的姑娘,也有嫁人的时候,作为XP的父母,MS微软明年四月将停止支持有十多年历史的 ...

  10. VS类自定义版权注释

    对IDE快捷方式右键-属性-打开文件位置,找到..\Microsoft Visual Studio 10.0\Common7\IDE文件夹下的..\ItemTemplates\CSharp\Code\ ...