zoukankan      html  css  js  c++  java
  • 洛谷—— P1168 中位数

    https://www.luogu.org/problem/show?pid=1168

    题目描述

    给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数。[color=red]即[/color]前1,3,5,……个数的中位数。

    输入输出格式

    输入格式:

    输入文件median.in的第1行为一个正整数N,表示了序列长度。

    第2行包含N个非负整数A[i] (A[i] ≤ 10^9)。

    输出格式:

    输出文件median.out包含(N + 1) / 2行,第i行为A[1], A[3], …, A[2i – 1]的中位数。

    输入输出样例

    输入样例#1:
    7
    1 3 5 7 9 11 6
    输出样例#1:
    1
    3
    5
    6

    说明

    对于20%的数据,N ≤ 100;

    对于40%的数据,N ≤ 3000;

    对于100%的数据,N ≤ 100000。

     1 #include <algorithm>
     2 #include <cstdio>
     3 #include <queue>
     4 
     5 using namespace std;
     6 
     7 priority_queue<int,vector<int>,less<int> >MAX;
     8 priority_queue<int,vector<int>,greater<int> >MIN;
     9 int ans,n,x,a[100000+15];
    10 
    11 
    12 int main()
    13 {
    14     scanf("%d",&n);
    15     for(int i=1;i<=n;i++) scanf("%d",a+i);
    16 //    sort(a+1,a+n+1);
    17     ans=a[1]; printf("%d",ans);
    18     for(int i=2;i<=n;i+=2)
    19     {
    20         if(i+1>n) break;
    21         for(int j=i;j<=i+1;j++)
    22             if(a[j]>ans) MIN.push(a[j]);
    23             else MAX.push(a[j]);
    24         int size=i>>1;
    25         if(MIN.size()>size) MAX.push(ans),ans=MIN.top(),MIN.pop();
    26         if(MAX.size()>size) MIN.push(ans),ans=MAX.top(),MAX.pop();
    27         printf("
    %d",ans);
    28     }
    29     return 0;
    30 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    113. Path Sum II
    112. Path Sum
    111. Minimum Depth of Binary Tree
    110. Balanced Binary Tree
    Create
    SetWindowPos
    INT_PTR数据类型
    SDK介绍
    COLORREF
    setfont()函数
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7203956.html
Copyright © 2011-2022 走看看