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 a[60], b[60], c[60];
int n, k;
vector <int> op;
bool alive() {
for (int i = 0; i < n; i++) {
if (a[i] >= k) return 0;
}
return 1;
}
void calc() {
int minj = 0x3f3f3f3f;
for (int i = 0; i < n; i++) {
if (a[i] == 0) a[i] = k, op.push_back(i << 1);
minj = min(minj, a[i]);
}
for (int i = 0; i < n; i++) {
a[i] -= minj;
}
}
int main () {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (k == n) {
printf("-1\n");
return 0;
}
while (!alive()) calc();
for (int i = 0; i < n; i++) {
b[i] = k - a[i] - 1;
}
int flag = 0;
for (int B = 0; B < k; B++) {
memset(c, 0, sizeof(c));
for (int i = 0; i < n; i++) {
b[i] = k - a[i] - 1;
}
for (int i = 0; i <= n - k; i++) {
c[i] = (B - b[i] + k) % k;
for (int j = i; j < i + k; j++) {
b[j] = (b[j] + c[i]) % k;
}
}
for (int i = 0; i < n; i++) {
if (b[i] != B) goto END;
}
flag = 1;
break;
END:;
}
if (!flag) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i <= n - k; i++) {
if (!c[i]) continue;
for (int j = 0; j < c[i]; j++) {
op.push_back(i << 1 | 1);
}
int maxj = -1;
for (int j = i; j < i + k; j++) {
maxj = max(maxj, a[j]);
}
maxj += c[i];
for (int j = 0; j < n; j++) {
if (j >= i && j < i + k) continue;
while (a[j] < maxj) a[j] += k, op.push_back(j << 1);
a[j] -= c[i];
}
while (!alive()) calc();
}
cout << op.size() << endl;
for (int i = 0; i < op.size(); i++) {
int x = op[i];
cout << (x & 1) + 1 << " " << (x >> 1) + 1 << endl;
}
return 0;
}
Compilation message (stderr)
joiris.cpp: In function 'int main()':
joiris.cpp:80:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = 0; i < op.size(); i++) {
| ~~^~~~~~~~~~~
# | 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... |