Codeforces 626E. Simple Skewness(三分)详细注释
来源:互联网 发布:网络售彩票最新消息 编辑:程序博客网 时间:2024/06/01 22:22
题目链接:http://codeforces.com/problemset/problem/626/E
E. Simple Skewness
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Input
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.
Output
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.
Examples
Input
4
1 2 3 12
Output
3
1 2 12
Input
4
1 1 2 2
Output
3
1 1 2
Input
2
1 2
Output
2
1 2
Note
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.
题意:给定一个序列a, 选取一些数,使得这些数的平均数减中位数尽可能大
思路:关键在于选取的数一定是奇数个,若为偶数个会使平均数减小而中位数不变因此将中位数个数枚举,再用三分法计算区间长度,通过排序贪心计算平均数就可以算出结果。
附上ac代码
#include<stdio.h>#include<algorithm>#include<string.h>#include<math.h>#define maxn 200005using namespace std;typedef long long ll;ll a[maxn], s[maxn];ll n;ll Cal(ll len, ll pos){ ll ans = s[pos] - s[pos - len - 1] + s[n] - s[n - len]; return ans;}int main(){ scanf("%I64d", &n); for(ll i = 1; i <= n; i++) scanf("%I64d", &a[i]); if(n == 1 || n == 2){ //当n的个数为1或2时只可能有一个答案 printf("1\n%I64d\n", a[1]); return 0; } sort(a + 1, a + 1 + n); //排序,贪心求和 ll len = 0, mid = 1, ma = 0; for(ll i = 1; i <= n; i++) //求前缀和 s[i] = s[i - 1] + a[i]; for(ll i = 2; i <= n - 1; i++){//枚举中位数 ll l = 0, r = min(i - 1, n - i); while(l < r){ //通过三分法求出区间长度 ll lmid = (2 * l + r) / 3; ll rmid = (l + 2 * r + 2) / 3; ll ans1 = Cal(lmid, i); ll ans2 = Cal(rmid, i); if(ans1 * (rmid * 2 + 1) < ans2 * (lmid * 2 + 1)) //为了避免分数,将除法转换为乘法 l = lmid + 1; else r = rmid - 1; } ll tmp = (s[i] - s[i - l - 1] + s[n] - s[n - l]) - a[i] * (l * 2 + 1); if(tmp * (len * 2 + 1) > ma * (l * 2 + 1)){ //更新最大值,以及中位数的位置 ma = tmp; //为了避免分数,将除法转换为乘法 len = l; mid = i; } } printf("%I64d\n", len * 2 + 1); for(ll i = mid - len; i <= mid; i++) printf("%I64d ", a[i]); for(ll i = n - len + 1; i <= n; i++) printf("%I64d ", a[i]); return 0;}
- Codeforces 626E. Simple Skewness(三分)详细注释
- CodeForces 626E Simple Skewness(三分)
- Codeforces 626E Simple Skewness(三分)
- 【CodeForces 626E】Simple Skewness (三分)
- [枚举+三分] Codeforces #626E. Simple Skewness
- CodeForces 626E Simple Skewness(二分)
- CodeForces 626E Simple Skewness
- codeforces 626E - Simple Skewness
- CodeForces 626E Simple Skewness
- CodeForce 626E Simple Skewness (贪心+三分)
- Codeforces 626E Simple Skewness 【二分】
- codeforces626E.Simple Skewness(三分)
- 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness(枚举+三分)
- 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness(枚举+三分)★
- codeforces Simple Skewness
- Simple Skewness CodeForces
- 8VC Venture Cup 2016 -E - Simple Skewness- 二分+数学
- Codeforces626E Simple Skewness(二分)
- rom和ram的区别
- Python之collections模块
- 服务器 客户端 ETAG 的作用
- C语言实现队列Queue
- 使用 Python 实现:对着电脑吼一声,自动打开浏览器中的默认网站。
- Codeforces 626E. Simple Skewness(三分)详细注释
- 数论小结+代码实现
- TF-IDF与余弦相似性的应用(一):自动提取关键词
- CDH5离线安装---使用的版本是5.11.1
- Oracle基础 物理备份 冷备份和热备份
- Linux (centos7) 防火墙命令
- set_exception_handler用法
- 为64位ARMv8架构开发板交叉编译openssl-1.0.2l
- SafeKit(DurexKit)实现原理