zoukankan      html  css  js  c++  java
  • (poj)Sequence Median

    Description

    Given a sequence of N nonnegative integers. Let's define the median of such sequence. If N is odd the median is the element with stands in the middle of the sequence after it is sorted. One may notice that in this case the median has position (N+1)/2 in sorted sequence if sequence elements are numbered starting with 1. If N is even then the median is the semi-sum of the two "middle" elements of sorted sequence. I.e. semi-sum of the elements in positions N/2 and (N/2)+1 of sorted sequence. But original sequence might be unsorted. 

    Your task is to write program to find the median of given sequence.

    Input

    The first line of input contains the only integer number N - the length of the sequence. Sequence itself follows in subsequent lines, one number in a line. The length of the sequence lies in the range from 1 to 250000. Each element of the sequence is a positive integer not greater than 2^32 - 1 inclusive.

    Output

    You should print the value of the median with exactly one digit after decimal point.

    Sample Input

    4
    3
    6
    4
    5
    

    Sample Output

    4.5

    Hint

    Huge input,scanf is recommended.
    AC code:
     1 #include<iostream>
     2 #include <cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     int nums[250001];
     8     int n;
     9     int i;
    10     while(scanf("%d",&n)!=EOF)
    11     {
    12         for(i=0;i<n;i++)
    13         {
    14             scanf("%d",&nums[i]);
    15         }
    16         sort(nums,nums+n);
    17         if(n%2==1)
    18             printf("%.1f
    ",double(nums[(n-1)/2]));
    19         if(n%2==0)
    20             printf("%.1f
    ",nums[n/2-1]/2.0+nums[n/2]/2.0);
    21     }
    22     return 0;
    23 }

    今天被这个题目坑了。

  • 相关阅读:
    人间故事馆话题:聊聊那些被骗经历,让其他人不再被骗
    路过的风景
    路过的风景
    上海最适合拍照的旅游地点
    Java EE (11)
    五、服务器端的局域网
    P1294 高手去散步 洛谷
    堆排序【模板】
    P3383 【模板】线性筛素数 洛谷
    P1516 青蛙的约会 洛谷
  • 原文地址:https://www.cnblogs.com/lzeffort/p/3477430.html
Copyright © 2011-2022 走看看