中位数
用stl的快速(On^2)来过数据不是特别强的Onlgn的方法
#include<bits/stdc++.h> using namespace std; int n,tmp,ans; priority_queue<int, vector<int>, greater<int> > mi;//存储更大的部分 priority_queue<int> ma;//存储更小的部分 int main() { int n; scanf("%d", &n); int x; for(int i = 1; i <= n; i++) { scanf("%d", &x); if(i & 1) { if(mi.empty()) mi.push(x); else { int tmp = ma.top(); if(x > tmp) { mi.push(x); } else { ma.pop(); ma.push(x); mi.push(tmp); } } printf("%d\n", mi.top()); } else { int tmp = mi.top(); if(x > tmp) { mi.pop(); mi.push(x); ma.push(tmp); } else { ma.push(x); } } } return 0; }

浙公网安备 33010602011771号