zoukankan      html  css  js  c++  java
  • codeforce 626E(二分)

    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 , 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

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cstring>
     5 using namespace std;
     6 typedef long long LL;
     7 const int Max = 200000 + 10;
     8 LL arr[Max],sum[Max];
     9 int main()
    10 {
    11     int n;
    12     scanf("%d", &n);
    13     sum[0] = 0;
    14     for(int i = 1; i <= n; i++)
    15     {
    16         scanf("%I64d", &arr[i]);
    17         sum[i] = sum[i - 1] + arr[i];
    18     }
    19     int res,pos = 1,len = 0;
    20     double maxRes = -1000000;
    21     for(int i = 1; i <= n; i++)
    22     {
    23         int l = 1, r = min(i - 1, n - i);
    24         res = 0;
    25         while(r >= l)
    26         {
    27             int mid = (l + r) >> 1;
    28             double mean1 = 1.0 * (sum[i] - sum[i - mid - 1] + sum[n] - sum[n - mid]) / (2 * mid + 1);
    29             double mean2 = 1.0 * (sum[i] - sum[i - mid] + sum[n] - sum[n - mid + 1]) / ( 2 * (mid - 1) + 1);
    30             if(mean1 > mean2)  //与前一个进行比较,如果比前一个大左边就往前移,否则往右移,因为平均数是先变大后变小的通过与前面的比较找到最大的那个;
    31             {
    32                 res = mid;
    33                 l = mid + 1;
    34             }
    35             else
    36             {
    37                 r = mid - 1;
    38             }
    39         }
    40         double ans = 1.0 * (sum[i] - sum[i - res - 1] + sum[n] - sum[n - res]) / (2 * res + 1) - arr[i];
    41         if(ans > maxRes)
    42         {
    43             maxRes = ans;
    44             pos = i;
    45             len = res;
    46         }
    47     }
    48     printf("%d
    ", 2 * len + 1);
    49    //题目对输出要求不严格,相同的结果随便输出
    50     for(int i = pos - len; i < pos; i++)
    51         printf("%I64d ", arr[i]);
    52     for(int i = n - len + 1; i <= n; i++)
    53         printf("%I64d ", arr[i]);
    54     printf("%I64d
    ", arr[pos]);
    55 
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    【转载】数据结构与算法设计
    【转载】简述Linux的启动过程
    【转载】20分钟MySQL基础入门
    【转载】linux内核笔记之进程地址空间
    【转载】linux内核笔记之高端内存映射
    Logical Address->Linear Address->Physical Address
    【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
    【转载】不会编程也能写爬虫?可视化爬虫工具是什么东东
    【转载】我是一个键盘
    80. Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5199031.html
Copyright © 2011-2022 走看看