codeforces626E.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
题意:给你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(三分)的更多相关文章
- 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 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- 【CodeForces 626E】Simple Skewness
题意 给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序). 分析 因为是子集,所以不一定是连续的序列.然后我们有下面几个结论. 1.最大偏度一 ...
- Codeforces 626E Simple Skewness 「数学」「二分」
题意: 给你一堆无序数,寻找它的一个子堆,使得子堆的平均数减中位数最大. 数字的个数n<=2e5 0<=xi<=1e6. 思路: 首先可以证明这堆数一定是奇数个,证明方法是尝试在奇数 ...
- codeforces 练习
codeforces 627 D. Preorder Test 二分 + 树dp 做logn次树dp codeforces 578D.LCS Again 给出一个字符串str,长度n<=10^6 ...
- 8VC Venture Cup 2016 - Elimination Round
在家补补题 模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, in ...
- codeforce 626E(二分)
E. Simple Skewness time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)
最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...
随机推荐
- EGADS框架处理流程分析
最近在搞异常检测相关的工作,因此调研了业界常用的异常检测系统.通过查阅相关资料,发现业界对雅虎开源的EGADS系统评价比较高,其git项目已有980个star.这周阅读了项目的源码,梳理了系统框架的基 ...
- MBAir下安装httprunner2.5.7 har2case 出现zsh: command not found解决方案
MBAir下python3.8安装httprunner2.5.7 出现zsh: command not found find / -name hrun查找到路径为: /Users/w550856/Li ...
- 克隆slave
在日常生活中,我们做的比较多的操作就是在线添加从库,比如线上有一主一丛两个数据库,由于业务的需要一台从库的读取量无法满足现在的需求,这样就需要我们在线添加从库,出于安全考虑,我们通常需要在从库上进行在 ...
- (十四)json、pickle与shelve模块
任何语言,都有自己的数据类型,那么不同的语言怎么找到一个通用的标准? 比如,后端用Python写的,前端是js,那么后端如果传一个dic字典给前端,前端肯定不认. 所以就有了序列化这个概念. 什么是序 ...
- 【Java】网络编程之NIO
简单记录 慕课网-解锁网络编程之NIO的前世今生 & 一站式学习Java网络编程 全面理解BIO/NIO/AIO 内容概览 文章目录 1.[了解] NIO网络编程模型 1.1.NIO简介 1. ...
- node爬虫 -- 网页图片
相信大家都听说过爬虫,我们也听说过Python是可以很方便地爬取网络上的图片,但是奈何本人不会Python,就只有通过 Node 来实践一下了. 接下来看我如何 板砖 ! !!
- synchronized的jvm源码分析聊锁的意义
上篇写完了ReentrantLock源码实现,从我们的角度分析设计锁,在对比大神的实现,顺道拍了一波道哥的马屁,虽然他看不到,哈哈.这一篇我们来聊一聊synchronized的源码实现,并对比reen ...
- 开源AwaitableCompletionSource,用于取代TaskCompletionSource
1 TaskCompletionSource介绍 TaskCompletionSource提供创建未绑定到委托的任务,任务的状态由TaskCompletionSource上的方法显式控制,以支持未来的 ...
- 手把手做一个基于vue-cli的组件库(上篇)
基于vue-cli4的ui组件库,先贴个最终效果吧,步骤有点多,准备分上下篇,上篇:如何做一个初步的组件.下篇:编写说明文档及页面优化.开工. GitHub源码地址:https://github.co ...
- 设置一个两边固定中间自适应的css
1.两边浮动,中间自动宽度 给左右两个盒子设置左右浮动,中间的盒子不设置宽度,左右两边边距为左右盒子的宽度,中间盒子的位置必须写在右盒子下面,不然会把右盒子挤下去 如: <div class ...