aboutsummaryrefslogtreecommitdiff
path: root/17/day2/lazybubblesort.cpp
diff options
context:
space:
mode:
Diffstat (limited to '17/day2/lazybubblesort.cpp')
-rw-r--r--17/day2/lazybubblesort.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/17/day2/lazybubblesort.cpp b/17/day2/lazybubblesort.cpp
new file mode 100644
index 0000000..8395298
--- /dev/null
+++ b/17/day2/lazybubblesort.cpp
@@ -0,0 +1,20 @@
+#include <bits/stdc++.h>
+using namespace std;
+
+int A[100001];
+
+int main() {
+ int N, K; cin >> N >> K;
+ for (int i = 0; i < N; ++i) cin >> A[i];
+ multiset<int> S;
+ vector<int> ans;
+ for (int i = 0; i < N; ++i) {
+ if (S.size() < K || A[i] > *begin(S)) {
+ if (S.size() == K) ans.push_back(*begin(S)), S.erase(begin(S));
+ S.insert(A[i]);
+ }
+ else ans.push_back(A[i]);
+ }
+ while (S.size()) ans.push_back(*begin(S)), S.erase(begin(S));
+ for (auto& x : ans) cout << x << '\n';
+} \ No newline at end of file