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;}
原创粉丝点击