Created
November 26, 2020 01:23
-
-
Save Thiago4532/0f6b90be36d3f1d041f1b0d9a6575a98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int maxn = 2e5 + 10; | |
int v[maxn]; | |
multiset<int> cc; | |
multiset<int>::iterator median; | |
void insert(int x) { | |
if (cc.empty()) { | |
median = cc.insert(x); | |
return; | |
} | |
auto v = *median; | |
cc.insert(x); | |
if (x >= v) { | |
if (cc.size()%2 == 1) | |
median++; | |
}else { | |
if (cc.size()%2 == 0) | |
median--; | |
} | |
} | |
void erase(int x) { | |
auto v = *median; | |
auto it = median; | |
if (x > v) { | |
if (cc.size()%2 == 1) | |
median--; | |
}else { | |
if (cc.size()%2 == 0) | |
median++; | |
} | |
if (x == v && cc.size()%2 == 1) median--; | |
if (x != v) it = cc.find(x); | |
cc.erase(it); | |
} | |
int main() { | |
int n, k; | |
cin >> n >> k; | |
for (int i = 1; i <= n; i++) | |
cin >> v[i]; | |
for (int i = 1; i <= k; i++) | |
insert(v[i]); | |
cout << *median << " "; | |
for (int i = 2; i <= n - k + 1; i++) { | |
erase(v[i-1]); | |
insert(v[i+k-1]); | |
cout << *median << " "; | |
} | |
cout << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment