打个 abc,给自己唐笑了。
最开始有一个数,每次添加两个数,之后求数列中位数。
给个简单做法,维护一个 set 和一根指针,指针指向中位数。
考虑指针每次添加两个数之后会怎么变化。
-
两个数都大于指针指向的数:指针指向的数就在中位数靠左一位,指针右移一位。
-
两个数一个大于指针指向的数,一个小于指针指向的数:指针指向的数依然是中位数。
-
两个数都小于指针指向的数:指针指向的数就在中位数靠右一位,指针左移一位。
维护好就行了,每次输出操作后指针指向的数。
#include <bits/stdc++.h>
#define E(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
using ll = long long;
int tt;
namespace p192 {
const int N = 1E6 + 10;
int x, q, a, b;
multiset <int> s;
inline void Main () {
cin >> x;
s.emplace(x);
auto IT = s.lower_bound(x);
cin >> q;
while(q --) {
cin >> a >> b;
s.emplace(a), s.emplace(b);
if(a > *IT && b > *IT) ++ IT;
else if(a < *IT && b < *IT) -- IT;
cout << *IT << "\n";
}
return void();
}
}
int main() {
tt = 1;
while(tt --) p192::Main();
return 0;
}