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的元素是多少.数 ...
随机推荐
- 20Mybatis_订单商品数据模型_一对一查询——resultType和resultMap两种方式以及两种方式的总结
上一篇文章分析了数据模型,这篇文章就给出一个需求,这个需求是一对一查询,并完成这个需求. ------------------------------------------------------- ...
- 23Spring_JdbcTemplate来实现单表的增删改查
第一步建表:
- 第五章 使用 Bootstrap Typeahead 组件(百度下拉效果)
推荐链接:http://www.cnblogs.com/haogj/p/3376874.html UnderScore官网:http://underscorejs.org/ 参考文档:http://w ...
- 【Mysql】日期时间格式化
1.日期转成指定格式:DATE_FORMAT(date, format) 2.int型时间转成指定格式:FROM_UNIXTIME(unix_timestamp,format) 根据format字符串 ...
- 【MFC】ID命名和数字约定
ID命名和数字约定 MFC ID 命名和数字约定需要满足以下要求: 提供对 Visual C++ 资源编辑器支持的 MFC 库和 MFC 应用程序中使用的一致的 ID 命名标准. 这样就可以轻松地对程 ...
- 加密方式&数字签名
1,对称加密 2,混合加密 3.数字签名 4,带加密的数字签名
- U3D Debug.log的问题
今天在测试有yield有关的问题时,发现Debug.log()是异步输出机制.不一定会在调用后立即执行. 在C++有类似问题:std::cout 也不一定会立即输出,加上"\n"或 ...
- MySQL基础 - 如何系统地学习数据库?
对于数据库的认知,除了大学的时候上过数据库这门课,留下的印象大概就是几条SQL语句一些模棱两可的基本概念,直到工作后面临使用场景才发现数据库的重要性.故归纳总结一下自己的数据库学习之路. 学习资源: ...
- System类及其常用函数
System 类包含一些有用的类字段和方法.它不能被实例化. 常用方法: 1.static void arraycopy(Object src, int srcPos, Object dest, in ...
- 集成架构:对比 Web API 与面向服务的架构和企业应用程序集成(转)
http://kb.cnblogs.com/page/521644/ 摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不 ...