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的元素是多少.数 ...
随机推荐
- RDLC系列之一 简单示例
参照文章:http://www.cnblogs.com/waxdoll/archive/2006/07/24/458409.html#!comments 一.效果图
- 【C#】【Thread】SynchronizationContext线程间同步
SynchronizationContext在通讯中充当传输者的角色,实现功能就是一个线程和另外一个线程的通讯. 需要注意的是,不是每个线程都附加SynchronizationContext这个对象, ...
- Linux网络共享管理(ssh,nfs,samba)
OpenSSH服务器: ssh协议:提供安全的ssh环境用于远程管理,默认端口:22: 服务名称:sshd openssh-server包的安装文件: 使用ssh客户端程序: 连接目标主机: ssh ...
- 检查c# 内存泄漏
c# 内存泄漏检查心得 系统环境 windows 7 x64 检查工具:ANTS Memory Profiler 7 或者 .NET Memory Profiler 4.0 开发的软件为winform ...
- Arduino小车学习与研究
信安系统设计基础实践模块 Arduino小车学习与研究 ================== 陈都(20135328) 余佳源(20135321) 莫凡(20135225) ---------- 索引 ...
- js验证身份证号
/* * 身份证检测(格式.地区.生日.年龄范围) * code:检测字符串 rangeAge:年龄范围[格式为:25-55] * 返回值 0:为空 ,不为0则验证不通过 */ : : : : : : ...
- 让C#开发更简单,抽象增删改
相信经常做一些MS,CRM 项目的童鞋非常有体会,大部分时间都是在复制和粘贴,大部分项目框架都是建一个三层,首先是DAL建一些增删改查,然后呢是BLL业务层再建一些增删改查,然后UI层再调用增删改查, ...
- 开源分布式计算引擎 & 开源搜索引擎 Iveely 0.5.0 为大数据而生
Iveely Computing 产生背景 08年的时候,我开始接触搜索引擎,当时遇到的第一个难题就是大数据实时并发处理,当时实验室的机器我们可以随便用,至少二三十台机器,可以,却没有程序可以将这些机 ...
- 第四十三课:jQuery插件化
我们先来看一个最简单的例子: (function($){ $.fn.extend({ //把此插件添加到jQuery的原型上 pluginName:function(){ //插件的名字 ...
- Spring学习进阶(一)初识Spring
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...