# [POJ 2833] The Average【二叉堆】

• 2018-01-21
• 0
• 0

## Problem:

 Time Limit: 6000MS Memory Limit: 10000K Case Time Limit: 4000MS

Description

In a speech contest, when a contestant finishes his speech, the judges will then grade his performance. The staff remove the highest grade and the lowest grade and compute the average of the rest as the contestant’s final grade. This is an easy problem because usually there are only several judges.

Let’s consider a generalized form of the problem above. Given n positive integers, remove the greatest n1 ones and the least n2 ones, and compute the average of the rest.

Input

The input consists of several test cases. Each test case consists two lines. The first line contains three integers n1n2 and n (1 ≤ n1n2 ≤ 10, n1 + n2 < n ≤ 5,000,000) separate by a single space. The second line contains n positive integers ai (1 ≤ ai ≤ 108 for all i s.t. 1 ≤ i ≤ n) separated by a single space. The last test case is followed by three zeroes.

Output

For each test case, output the average rounded to six digits after decimal point in a separate line.

Sample Input

```1 2 5
1 2 3 4 5
4 2 10
2121187 902 485 531 843 582 652 926 220 155
0 0 0```

Sample Output

```3.500000
562.500000```

Hint

This problem has very large input data. scanf and printf are recommended for C++ I/O.

The memory limit might not allow you to store everything in the memory.

Source

POJ Monthly--2006.05.28, zby03

## Code: O(Tnlogn) [644K, 407MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

inline int getint(int &num){
char ch;
while(!isdigit(ch = getchar()));
num = ch - '0';
while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
return num;
}

int n1, n2, n, a;

struct Min_Heap{
int node[1 << 5], size;

inline void clear() {size = 0;}

inline void shift_up(int u){
for(; u >> 1; u >>= 1)
if(node[u] < node[u >> 1]) swap(node[u], node[u >> 1]);
else break;
}

inline void shift_down(int u){
int v;
for(; (u << 1) <= size; u = v){
v = u << 1;
if(v + 1 <= size && node[v + 1] < node[v]) v++;
if(node[u] > node[v]) swap(node[u], node[v]);
else break;
}
}

inline void push(const int &key){
node[++size] = key;
shift_up(size);
}

inline void pop(){
node[1] = node[size--];
shift_down(1);
}

inline int top() {return node[1];}
} high;

struct Max_Heap{
int node[1 << 5], size;

inline void clear() {size = 0;}

inline void shift_up(int u){
for(; u >> 1; u >>= 1)
if(node[u] > node[u >> 1]) swap(node[u], node[u >> 1]);
else break;
}

inline void shift_down(int u){
int v;
for(; (u << 1) <= size; u = v){
v = u << 1;
if(v + 1 <= size && node[v + 1] > node[v]) v++;
if(node[u] < node[v]) swap(node[u], node[v]);
else break;
}
}

inline void push(const int &key){
node[++size] = key;
shift_up(size);
}

inline void pop(){
node[1] = node[size--];
shift_down(1);
}

inline int top() {return node[1];}
} low;

int main(){
while(getint(n1) && getint(n2) && getint(n)){
long long sum = 0;
for(register int i = 1; i <= n; i++){
sum += getint(a);
low.push(a), high.push(a);
if(high.size > n1) high.pop();
if(low.size > n2) low.pop();
}
while(low.size) sum -= low.top(), low.pop();
while(high.size) sum -= high.top(), high.pop();
double ave = (double)sum / (n - n1 - n2);
printf("%.6f\n", ave);  // The format of double in printf() must be "%f"
}
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai