Calculate the median of a large number.
Train of Thought: This question cannot store all elements; otherwise, MLE will be used, and priority queue will be used (the default value is higher than the default value) will be used to store half of the content. The remaining half will be compared with the first half of the team, if the number is smaller than the first, the first element of the team will be removed and the new element will be added to the team.
Finally, if n is an odd number, the first element of the queue is the median value. If n is an even number, the average values of the first two of the queues are the answers.
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef unsigned int lint;priority_queue<lint> pq;int main(){ int i, n; lint val, t; scanf("%d", &n); for (i = 0; i <= n/2; ++i) { scanf("%d", &val); pq.push(val); } for (; i < n; ++i) { scanf("%d", &val); t = pq.top(); if (val < t) { pq.pop(); pq.push(val); } } double ans; if (n&1) ans = pq.top(); else { ans = pq.top(); pq.pop(); ans += pq.top(); ans /= 2.0; } printf("%.1lf\n", ans); return 0;}