codeforce 626E(二分)
3 seconds
256 megabytes
standard input
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.
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.
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.
4 1 2 3 12
3 1 2 12
4 1 1 2 2
3 1 1 2
2 1 2
2 1 2
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(二分)的更多相关文章
- codeforce 359D 二分+ 动态规划(sparse table)
原题链接:http://codeforces.com/problemset/problem/359/D 思路:首先对符合题目的长度(r-l)从0到n-1进行二分查找,对每一个长度进行check,看是否 ...
- Codeforces 626E Simple Skewness 「数学」「二分」
题意: 给你一堆无序数,寻找它的一个子堆,使得子堆的平均数减中位数最大. 数字的个数n<=2e5 0<=xi<=1e6. 思路: 首先可以证明这堆数一定是奇数个,证明方法是尝试在奇数 ...
- codeforce 702C Cellular Network 二分答案
http://codeforces.com/contest/702 题意:n个村庄,m个机站,问机站最短半径覆盖完所有村庄 思路:直接二分答案 二分太弱,调了半天..... // #pragma co ...
- codeforce 955c --Sad powers 思路+二分查找
这一题的题意是 定义一个数,该数特点是为a的p次方 (a>0,p>1) 再给你n个询问,每个询问给出一个区间,求区间内该数的数目. 由于给出的询问数极大(10e5) 所以,容易想到应该 ...
- 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 ...
- 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 ...
- [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 ...
- 二分题 D - Salary Changing codeforce
题意:给出n个人(n是奇数),s钱:s为总的可以付工钱的钱: 每一个工人有一个付工钱的区间,只要在这个区间范围内,随便一个数都可以当作给这个工人付了钱: 老板要付给每个工人钱,并且付钱的中位数要尽可能 ...
- Codeforce 380A Sereja and Prefixes【二分】
题意:定义两种操作 1 a ---- 向序列中插如一个元素a 2 a b ---- 将序列的前a个元素[e1,e2,...,ea]重复b次插入到序列中 经过一列操作后,为处于某个位置p的元素是多少.数 ...
随机推荐
- HTML5-WebSocket技术学习(2)
上一篇介绍了websocket的基本用法.这篇介绍websocket的一个框架: socket.io socket.io是一个既可以用在客户端又可以用在服务器端的框架. 本篇介绍socket.io在客 ...
- python中的Unittest常用方法
import unittest class SimpleUnitTest(unittest.TestCase): def test_Fail(self): self.failUnless(True) ...
- PHP header函数的几大作用
先看看官方文档的定义 (PHP 4, PHP 5, PHP 7) header - 发送原生 HTTP 头 void header ( string $string [, bool $replace ...
- google project tango 学习笔记
google io 2015上 project tango 的演示视频
- 加密算法使用(四):AES的使用
AES是一种对称加密方式,比DES更为安全,用一个秘钥加密数据之后,可以用同一个秘钥对加密后的数据解密还原,以下是一套以字符串为例子的使用全过程演示, 用到了 commons-codec.jar pa ...
- angularjs中只显示选中的radio的值
angularjs中,只显示选中的radio的值.主要是相同的radio,name属性值要相同还有ng-model的值要相同,同时要指定value值.这样选中的时候就会在下面的div中显示选中的值了. ...
- 集成架构:对比 Web API 与面向服务的架构和企业应用程序集成(转)
http://kb.cnblogs.com/page/521644/ 摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不 ...
- Java系列,《Java核心技术 卷1》,chapter 13,集合
13.1.2 Java类库中的集合接口和迭代器接口 删除元素,对于next和remove的调用是互相依赖的,如果调用remove之前没有调用next,则会跑出IllegalStateExcep ...
- 《Linux及安全》实验安排
SEED(SEcurity EDucation)项目由雪城大学杜文亮教授2002年创立,为计算机教学提供一套信息安全实验环境,目前已开发超过30个实验,涵盖广泛的安全原理,被全世界数百个高校采用. 实 ...
- Opencv step by step - 视频变换
这次是变换视频内容并且写入新的文件. 代码如下: note:因为代码比较少,前几篇博客的代码都是手打的,并且做了一些修改. #include <cv.h> #include <hig ...