您的位置:首页 > 其它

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
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: