codeforce 626E(二分)
2016-02-18 18:30
281 查看
E. Simple Skewness
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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
Output
Input
Output
Input
Output
Note
In the first case, the optimal subset is
View Code
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; typedef long long LL; const int Max = 200000 + 10; LL arr[Max],sum[Max]; int main() { int n; scanf("%d", &n); sum[0] = 0; for(int i = 1; i <= n; i++) { scanf("%I64d", &arr[i]); sum[i] = sum[i - 1] + arr[i]; } int res,pos = 1,len = 0; double maxRes = -1000000; for(int i = 1; i <= n; i++) { int l = 1, r = min(i - 1, n - i); res = 0; while(r >= l) { int mid = (l + r) >> 1; double mean1 = 1.0 * (sum[i] - sum[i - mid - 1] + sum - sum[n - mid]) / (2 * mid + 1); double mean2 = 1.0 * (sum[i] - sum[i - mid] + sum - sum[n - mid + 1]) / ( 2 * (mid - 1) + 1); if(mean1 > mean2) //与前一个进行比较,如果比前一个大左边就往前移,否则往右移,因为平均数是先变大后变小的通过与前面的比较找到最大的那个; { res = mid; l = mid + 1; } else { r = mid - 1; } } double ans = 1.0 * (sum[i] - sum[i - res - 1] + sum - sum[n - res]) / (2 * res + 1) - arr[i]; if(ans > maxRes) { maxRes = ans; pos = i; len = res; } } printf("%d\n", 2 * len + 1); //题目对输出要求不严格,相同的结果随便输出 for(int i = pos - len; i < pos; i++) printf("%I64d ", arr[i]); for(int i = n - len + 1; i <= n; i++) printf("%I64d ", arr[i]); printf("%I64d\n", arr[pos]); return 0; }
View Code
相关文章推荐
- 《构建之法》阅读梳理篇(第一遍阅读)
- 四款工具顺利实现 Python 与 JavaScript 间的代码转换
- 网络数据缓存
- [转]Android How to Download and Make Volley.jar
- React等开发工具记录
- ITIL与ITSM的联系与区别
- Android自定义对话框(Dialog)位置,大小
- Ext.isEmpty( Mixed value, [Boolean allowBlank] ) 用法
- U3d内存优化(二)之Dictonary
- [Html]Jekyll 代码高亮的几种选择
- 实用干货参考
- 大型web系统架构详解
- XCode真机测试发布时产生The executable was signed with invalid entitlements.解决办法
- Android触摸屏事件派发机制一(View篇)
- 《Linux系统useradd-passwd添加账号密码讲解及su命令介绍》
- 使用GLEW注意事项
- QT 计算执行时间 ms
- jquery中onchange和onclick的使用
- 《Effective java》读书记录-第26条-优先考虑泛型
- iOS8:TouchID