This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n), b(k);
for (int i = 0; i < n; ++i) {
cin >> a[i];
b[i % k] += a[i];
}
for (int i = 0; i < k; ++i) {
b[i] %= k;
}
const int ost = n % k;
for (int i = 0; i < ost - 1; ++i) {
if (b[i] != b[i + 1]) {
cout << -1;
return 0;
}
}
for (int i = ost; i < k - 1; ++i) {
if (b[i] != b[i + 1]) {
cout << -1;
return 0;
}
}
vector<pair<int, int>> ans;
auto vertical = [&](int i) {
assert(i >= 0 && i < n);
a[i] += k;
ans.emplace_back(1, i + 1);
};
auto horizontal = [&](int i) {
assert(i >= 0 && i + k <= n);
for (int j = i; j < i + k; ++j) {
a[j] += 1;
}
ans.emplace_back(2, i + 1);
};
auto clear = [&] {
int mn = *min_element(a.begin(), a.end());
for (int &i: a) {
i -= mn;
}
};
auto normalize = [&] {
clear();
int mx = *max_element(a.begin(), a.end());
for (int i = 0; i < n; ++i) {
while (a[i] < mx) {
vertical(i);
}
}
clear();
assert(*max_element(a.begin(), a.end()) < k);
};
normalize();
for (int i = 1; i < n; ++i) {
while (a[i] < a[i - 1]) {
vertical(i);
}
}
assert(is_sorted(a.begin(), a.end()));
int need = a[n - 1];
for (int x = 1; x <= need; ++x) {
for (int i = n - 1; i - k >= -1;) {
if (a[i] < x) {
horizontal(i - k + 1);
i -= k;
} else {
i -= 1;
}
}
}
for (int i = k - 1; i < n - 1; ++i) {
assert(a[i] == a[i + 1]);
}
need = a[n - 1];
for (int i = 0; i < k - 1; ++i) {
while (a[i] < need) {
vertical(i);
}
}
clear();
for (int i = k - 1; i < n; ++i) {
assert(a[i] == 0);
}
int mx = *max_element(a.begin() + ost, a.end());
for (int i = ost; i < n; ++i) {
assert(a[i] % k == 0);
while (a[i] < mx) {
vertical(i);
}
}
for (int i = 0; i < ost; ++i) {
while (a[i] < mx) {
vertical(i);
}
}
clear();
if (ost > 0) {
mx = *max_element(a.begin(), a.end());
for (int i = 0; i < ost; ++i) {
assert((mx - a[i]) % k == 0);
while (a[i] < mx) {
vertical(i);
}
}
int i = ost;
for (; i < n; i += k) {
while (a[i] < mx) {
horizontal(i);
}
}
assert(i == n);
clear();
}
for (int i = 0; i < n; ++i) {
assert(a[i] == 0);
}
cout << (int)ans.size() << '\n';
for (auto [tp, i] : ans) {
cout << tp << " " << i << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |