Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- #include <set>
- #include <array>
- using namespace std;
- const int maxn = 2e5 + 10;
- multiset<int> smaller_values, greater_values;
- int k;
- void insert_value(int x) {
- int max_in_smaller = *smaller_values.rbegin();
- if(max_in_smaller < x) {
- greater_values.insert(x);
- if(greater_values.size() > k / 2) {
- int min_in_greater = *greater_values.begin();
- smaller_values.insert(min_in_greater);
- greater_values.erase(greater_values.find(min_in_greater));
- }
- }
- else {
- smaller_values.insert(x);
- if(smaller_values.size() > (k + 1) / 2) {
- int tmp_max_in_smaller = *smaller_values.rbegin();
- greater_values.insert(tmp_max_in_smaller);
- smaller_values.erase(smaller_values.find(tmp_max_in_smaller));
- }
- }
- }
- void remove_value(int x) {
- if(greater_values.find(x) != greater_values.end()) {
- greater_values.erase(greater_values.find(x));
- }
- else if(smaller_values.find(x) != smaller_values.end()) {
- smaller_values.erase(smaller_values.find(x));
- }
- if(smaller_values.empty()) {
- smaller_values.insert(*greater_values.begin());
- greater_values.erase(greater_values.begin());
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n >> k;
- vector<int> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- smaller_values.insert(v[0]);
- for(int i = 1; i < k; i++) {
- insert_value(v[i]);
- }
- cout << *smaller_values.rbegin() << " ";
- for(int i = k; i < n; i++) {
- if(k > 1) {
- remove_value(v[i - k]);
- insert_value(v[i]);
- }
- else {
- insert_value(v[i]);
- remove_value(v[i - 1]);
- }
- cout << *smaller_values.rbegin() << " ";
- }
- return 0;
- }
- // 5, 1, 2, 3
- // 1, 2, 3, 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement