Codeforces 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness
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.
题目链接:http://codeforces.com/contest/626/problem/E
题意
给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序)。
分析
因为是子集,所以不一定是连续的序列。然后我们有下面几个结论。
1.最大偏度一定≥0
因为一个元素时,偏度为0。
2.最大偏度子集必定有元素个数为奇数个的。
证:
如果当元素个数是偶数2*k时偏度最大,我们证明它去掉一个元素a[k+1]不会更差。
子集里排好序分别是a[i]。除去a[k+1]其它数的平均值为av
新平均值-旧平均值=av-(av+a[k+1])/2=(av-a[k+1])/2
新中位数-旧中位数=a[k]-(a[k]+a[k+1])/2=(a[k]-a[k+1])/2
且有 旧平均值-旧中位数=(av+a[k+1])/2-(a[k]+a[k+1])/2=(av-a[k])/2≥0 (否则不可能偏度最大)
所以有 平均值增量-中位数增量=(av-a[k])/2≥0
所以新的偏度肯定不会更差。
3.平均值先递增后递减
因为是奇数个,所以枚举每个数做中位数,假如左右延伸长度为j,那么要使偏度更大,我们一定是每次选剩下的里面左边最大和右边最大的数。所以剩下的数越来越小,平均值增加得越来越少,而当前平均值越来越大,到某个峰值后平均值就开始减小了。
所以可以用二分法每次取中点和中点旁边一个点判断当前平均值在增加还是减小,增加就往右找,减小就往左找。
顺便吐槽一句:cf测评机不行啊,跑这个跑了大半个小时才跑完!!!

下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
int n;
ll s[N<<],a[N<<];
int ansi=,ansp;
double ans;
//最大偏度≥0,所以初始值就是只有第一个元素,偏度为0
ll sum(int i,int j)
{
return s[n]-s[n-j]+s[i]-s[i-j-];
}
int main()
{
n=read();
for(int i=;i<=n;i++)
cin>>a[i];
sort(a+,a++n);
for(int i=;i<=n;i++)
{
s[i]=s[i-]+a[i];
}
for(int i=;i<n;i++)
{
int l=,r=min(i-,n-i),m;
ll s1,s2;
while(l<r)
{
int mid=(l+r)/;
s1=sum(i,mid)*(*mid+);
s2=sum(i,mid+)*(*mid+);
if(s1>s2)
{
r=mid;
}
else
{
l=mid+;
if(s1==s2)
break;
}
}
if(1.0*sum(i,l)/(*l+)-a[i]>ans)
{
ans=1.0*sum(i,l)/(*l+)-a[i];
ansi=i;
ansp=l;
}
}
cout<<ansp*+<<endl;
cout<<a[ansi]<<" ";
for(int i=;i<=ansp;i++)
{
cout<<a[ansi-i]<<" "<<a[n-i+]<<" ";
}
cout<<endl;
return ;
}
Codeforces 626E Simple Skewness(暴力枚举+二分)的更多相关文章
- Codeforces 626E Simple Skewness 「数学」「二分」
题意: 给你一堆无序数,寻找它的一个子堆,使得子堆的平均数减中位数最大. 数字的个数n<=2e5 0<=xi<=1e6. 思路: 首先可以证明这堆数一定是奇数个,证明方法是尝试在奇数 ...
- codeforces 626E. Simple Skewness 三分
题目链接 给n个数, 让你去掉一些数, 使得剩下的数的平均值-中位数的差值最大. 先将数组排序, 然后枚举每一个数作为中位数的情况, 对于每个枚举的数, 三分它的左右区间长度找到一个平均值最大的情况, ...
- 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness 暴力+二分
E. Simple Skewness 题目连接: http://www.codeforces.com/contest/626/problem/E Description Define the simp ...
- Codeforces C. Maximum Value(枚举二分)
题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces Restore Cube(暴力枚举)
/* 题意:给出立方体的每个顶点的坐标(是由源坐标三个数某几个数被交换之后得到的!), 问是否可以还原出一个立方体的坐标,注意这一句话: The numbers in the i-th output ...
- codeforces 700C Break Up 暴力枚举边+边双缩点(有重边)
题意:n个点,m条无向边,每个边有权值,给你 s 和 t,问你至多删除两条边,让s,t不连通,问方案的权值和最小为多少,并且输出删的边 分析:n<=1000,m是30000 s,t有4种情况( ...
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- CodeForces 496D Tennis Game (暴力枚举)
题意:进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时, 游戏结束.现在给出n次对决的记录,问可能的s和t有多少种,并按s递增 ...
- Codeforces 327A-Flipping Game(暴力枚举)
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
随机推荐
- Mac shell笔记
用来自动执行一些前端发布的操作. 脚本: # webReleasePath用来发布的目录,webRevisionPath是开发的目录 webReleasePath='/Users/lufeng/Doc ...
- Spark源码剖析(一):如何将spark源码导入到IDEA中
由于近期准备深入研究一下Spark的核心源码,所以开了这一系列用来记录自己研究spark源码的过程! 想要读源码,那么第一步肯定导入spark源码啦(笔者使用的是IntelliJ IDEA),在网上找 ...
- ArcGIS API for JavaScript 4.2学习笔记[14] 弹窗的位置、为弹窗添加元素
这一节我们来看看弹窗的位置和弹窗上能放什么. 先一句话总结: 位置:可以随便(点击时出现或者一直固定在某个位置),也可以指定位置 能放什么:四种,文字.媒体(图片等).表格.附件. [Part I 位 ...
- 【ASP.NET MVC系列】浅谈NuGet在VS中的运用
一 概述 在我们讲解NuGet前,我们先来看看一个例子. 1.例子: 假设现在开发一套系统,其中前端框架我们选择Bootstrap,由于选择Bootstrap作为前端框架,因此,在项目中,我们 ...
- 设计模式之 - 代理模式(Proxy Pattern)
代理模式:代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委托类预处理消息,过滤消息并转发消息,以及进行消息被委托类执行后的后续处理.很多可以框架中都有用 ...
- Dubbo(二) 认识Zookeeper
前言 在昨天,我们给大家基本介绍了Dubbo,文中反复提到了Zookeeper,那么它到底是什么呢,这篇文章我们将从Dubbo层面去了解Zookeeper,不做全面讲解,毕竟这是Dubbo教程啊~ Z ...
- webpack配置报错:invalid configuration object.webpack has been initialisted using a configuration objcet that does not match thie API schema
最近接收了别人的项目,webpack配置总是报错如下:最后找到了解决办法,在此分享一下: 错误情况: 解决办法: 将package.json里面的colors删除掉即可
- 《MYSQL》----字符串的复杂函数,检索的七-天-排-重
接到了一个新的需求,拿到需求的时候瞬间有点头大,因为实在是有些棘手. 我们这个系统本身是个接口系统,总接口数大概在200个左右.外部会有很多用户在 不同的时间拿着不同参数去调我们的这些接口,用户的调集 ...
- 漂亮的提示框SweetAlert使用教程
一.简介 所使用过的弹出框插件,SweetAlert是最好用的.发展至今,已经有两个版本,一个是原版 t4t5/sweetalert , 一个是分支版 limonte/sweetalert2 ,更新相 ...
- SSM框架开发web项目系列(五) Spring集成MyBatis
前言 在前面的MyBatis部分内容中,我们已经可以独立的基于MyBatis构建一个数据库访问层应用,但是在实际的项目开发中,我们的程序不会这么简单,层次也更加复杂,除了这里说到的持久层,还有业务逻辑 ...