[POJ 2833] The Average【二叉堆】
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 n1, n2 and n (1 ≤ n1, n2 ≤ 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
Solution:
大水题。
由于 n 的数据范围达到 5e6,直接开数组需要的空间至少为 19532 KB,MLE。
而 n1 和 n2 最大只有 10,我们可以考虑用两个堆分别维护最大的 n1 个数及最小的 n2 个数。
出于良心考虑,我没有用 STL 里的 priority_queue,而是手打了两个堆(实际上第二个堆是第一个复制过去改的)。。
然而最后竟然因为输出 double 类型的时候用了 %.6lf 炸掉了,没有 1A。。大家一定要注意 g++ 标准里 double 的格式符只有在 scanf() 里用 %lf,在 printf() 里一定要用 %f。。
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; }
发表评论