题目有提示内存限制,所以自然会想到用优先队列来维护前k大和前k小。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using namespace std; 6 7 typedef long long ll; 8 priority_queue<int, vector<int>, greater<int> > s; 9 priority_queue<int, vector<int>, less<int> > b; 10 11 int main () 12 { 13 int n1, n2, n; 14 while ( scanf("%d%d%d", &n1, &n2, &n) != EOF ) 15 { 16 if ( !n1 && !n2 && !n ) break; 17 ll ans = 0; 18 for ( int i = 0; i < n; i++ ) 19 { 20 int tmp; 21 scanf("%d", &tmp); 22 ans += tmp; 23 if ( s.size() < n1 ) 24 { 25 s.push(tmp); 26 } 27 else 28 { 29 if ( tmp > s.top() ) 30 { 31 s.pop(); 32 s.push(tmp); 33 } 34 } 35 if ( b.size() < n2 ) 36 { 37 b.push(tmp); 38 } 39 else 40 { 41 if ( tmp < b.top() ) 42 { 43 b.pop(); 44 b.push(tmp); 45 } 46 } 47 } 48 while ( !s.empty() ) 49 { 50 ans -= s.top(); 51 s.pop(); 52 } 53 while ( !b.empty() ) 54 { 55 ans -= b.top(); 56 b.pop(); 57 } 58 double avg = ans * 1.0 / ( n - n1 - n2 ); 59 printf("%.6f\n", avg); 60 } 61 return 0; 62 }
时间: 2024-10-18 06:58:50