Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
- int n, k;
- cin >> n >> k;
- vector<int> a(n); for (auto& it : a) cin >> it;
- vector<int> group(256, -1);
- vector<int> gsize(256, 0);
- function<void(int)> add_group_for_element = [&](int element) {
- //cout << "add for it = " << element << endl;
- int left = element;
- while (left >= 0 && element - left < k && group[left] == -1) {
- --left;
- }
- if (left >= 0 && group[left] != -1) {
- if (gsize[group[left]] + element - left <= k) {
- for (int i = left+1; i <= element; ++i) {
- group[i] = group[left];
- gsize[group[i]]++;
- assert(gsize[group[i]] <= k);
- }
- return;
- }
- }
- ++left;
- // cout << "left = " << left << endl;
- for (int i = left; i <= element; ++i) {
- group[i] = left;
- gsize[left]++;
- //cout << "\tgroup[" << i << "] = " << left << endl;
- }
- };
- for (auto& it : a) {
- if (group[it] == -1) {
- add_group_for_element(it);
- }
- assert(group[it] != -1);
- it = group[it];
- cout << it << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement