#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
int A[50];
vpii sol;
int norm(int N,int K) {
int i;
while (1) {
for (i = 0; i < N; i++) {
if (A[i] >= K) break;
}
if (i < N) {
int m = 99;
for (i = 0; i < N; i++) {
if (A[i] == 0) {
sol.pb(mp(1,i+1));
A[i] += K;
}
m = min(m,A[i]);
}
for (i = 0; i < N; i++) A[i] -= m;
}
else break;
}
return 0;
}
int diff[60];
int main() {
int i;
int N,K;
scanf("%d %d",&N,&K);
for (i = 0; i < N; i++) scanf("%d",&A[i]);
if (K == 2) {
if ((N & 1) == 0) {
int s = 0;
for (i = 0; i < N; i++) s += A[i];
if (s & 1) {
printf("-1\n");
return 0;
}
while (1) {
for (i = 0; i < N; i++) {
if (A[i] >= 2) break;
}
if (i < N) {
int m = 99;
for (i = 0; i < N; i++) {
if (A[i] == 0) {
sol.pb(mp(1,i+1));
A[i] += 2;
}
m = min(m,A[i]);
}
for (i = 0; i < N; i++) A[i] -= m;
}
else break;
}
while (1) {
vi v;
for (i = 0; i < N; i++) {
if (A[i] > 0) v.pb(i);
}
if (v.empty()) break;
for (i = 1; i < v.size(); i++) {
if (((v[i]-v[i-1]) & 1) == 1) break;
}
if (i >= v.size()) {
int x = v[0]/2;
sol.pb(mp(1,2*x+1));
sol.pb(mp(1,2*x+2));
for (i = 0; i < N/2; i++) {
if (i != x) {
sol.pb(mp(2,2*i+1));
if (!A[2*i] && !A[2*i+1]) {
sol.pb(mp(2,2*i+1));
A[2*i] = A[2*i+1] = 1;
}
}
}
A[2*x] ^= 1,A[2*x+1] ^= 1;
for (i = 0; i < N; i++) {
if ((i != 2*x) && (i != 2*x+1)) {
if (!A[i]) sol.pb(mp(1,i+1));
}
}
for (i = 0; i < N; i++) {
if ((i != v[0]) && A[i]) sol.pb(mp(1,i+1));
}
}
else {
int l = v[i-1],r = v[i];
A[l]--,A[r]--;
for (i = 0; i < N; i++) {
if ((i >= l) && (i <= r)) {
if ((i < r) && ((i-l) & 1)) sol.pb(mp(2,i+1));
}
else sol.pb(mp(1,i+1));
}
for (i = l; i <= r; i += 2) sol.pb(mp(2,i+1));
}
}
}
else {
while (1) {
for (i = 0; i < N; i++) {
if (A[i] >= 2) break;
}
if (i < N) {
int m = 99;
for (i = 0; i < N; i++) {
if (A[i] == 0) {
sol.pb(mp(1,i+1));
A[i] += 2;
}
m = min(m,A[i]);
}
for (i = 0; i < N; i++) A[i] -= m;
}
else break;
}
while (1) {
for (i = 0; i < N-1; i += 2) {
if (A[i] != A[i+1]) break;
}
if (i >= N-1) {
for (i = 0; i < N-1; i += 2) {
if (A[i] == 0) sol.pb(mp(2,i+1));
}
if (A[N-1] == 0) {
sol.pb(mp(1,N));
for (i = 0; i < N-1; i += 2) sol.pb(mp(2,i+1));
}
break;
}
else {
if (A[i] == 1) {
for (i = 0; i < N; i++) {
if (A[i] == 0) sol.pb(mp(1,i+1));
A[i] ^= 1;
}
}
else {
int x = i;
for (i = 0; i < x; i += 2) {
sol.pb(mp(2,i+1));
if ((A[i] == 0) && (A[i+1] == 0)) {
sol.pb(mp(2,i+1));
A[i] = A[i+1] = 1;
}
}
for (i = x+1; i < N; i += 2) {
sol.pb(mp(2,i+1));
if ((A[i] == 0) && (A[i+1] == 0)) {
sol.pb(mp(2,i+1));
A[i] = A[i+1] = 1;
}
}
sol.pb(mp(1,x+1));
A[x] = 1;
}
}
}
}
}
else {
int j;
norm(N,K);
for (i = 0; i <= N; i++) {
if (i == 0) diff[i] = -A[i];
else if (i == N) diff[i] = A[N-1];
else diff[i] = A[i-1]-A[i];
//cout<<diff[i]<<endl;
}
for (i = 0; i <= N-K; i++) {
if (((diff[i]+diff[i+K]+3*K) % K) != 0) break;
}
if (i <= N-K) {
printf("-1\n");
return 0;
}
else {
for (i = 0; i <= N-K; i++) {
int m = 0;
for (j = i; j < i+K; j++) m = max(m,A[j]);
int x = (diff[i]+K) % K;
for (j = 0; j < x; j++) sol.pb(mp(2,i+1));
m += x;
for (j = 0; j < N; j++) {
if ((j < i) || (j >= i+K)) {
while (A[j] < m) sol.pb(mp(1,j+1)),A[j] += K;
}
else A[j] += x;
}
m = 1e9;
for (j = 0; j < N; j++) m = min(m,A[j]);
for (j = 0; j < N; j++) A[j] -= m;
norm(N,K);
//for(j=0;j<N;j++)cout<<A[j]<<" ";
//cout<<endl;
}
}
while (A[0] != 0) {
for (i = 0; i < N; i++) {
A[i]++,A[i] %= K;
if ((i % K) == 0) sol.pb(mp(2,i+1));
}
}
}
printf("%d\n",sol.size());
for (i = 0; i < sol.size(); i++) printf("%d %d\n",sol[i].first,sol[i].second);
return 0;
}
Compilation message
joiris.cpp: In function 'int main()':
joiris.cpp:70:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 1; i < v.size(); i++) {
~~^~~~~~~~~~
joiris.cpp:73:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i >= v.size()) {
~~^~~~~~~~~~~
joiris.cpp:216:29: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<int, int> >::size_type {aka long unsigned int}' [-Wformat=]
printf("%d\n",sol.size());
~~~~~~~~~~^
joiris.cpp:217:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < sol.size(); i++) printf("%d %d\n",sol[i].first,sol[i].second);
~~^~~~~~~~~~~~
joiris.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&N,&K);
~~~~~^~~~~~~~~~~~~~~
joiris.cpp:37:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < N; i++) scanf("%d",&A[i]);
~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
508 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
3 ms |
584 KB |
Output is correct |
10 |
Correct |
3 ms |
728 KB |
Output is correct |
11 |
Correct |
3 ms |
728 KB |
Output is correct |
12 |
Correct |
3 ms |
728 KB |
Output is correct |
13 |
Correct |
3 ms |
728 KB |
Output is correct |
14 |
Correct |
3 ms |
728 KB |
Output is correct |
15 |
Correct |
3 ms |
728 KB |
Output is correct |
16 |
Correct |
2 ms |
728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
728 KB |
Output is correct |
2 |
Correct |
3 ms |
728 KB |
Output is correct |
3 |
Correct |
2 ms |
728 KB |
Output is correct |
4 |
Correct |
2 ms |
728 KB |
Output is correct |
5 |
Correct |
3 ms |
728 KB |
Output is correct |
6 |
Correct |
3 ms |
728 KB |
Output is correct |
7 |
Correct |
2 ms |
728 KB |
Output is correct |
8 |
Correct |
2 ms |
728 KB |
Output is correct |
9 |
Correct |
3 ms |
728 KB |
Output is correct |
10 |
Correct |
2 ms |
728 KB |
Output is correct |
11 |
Correct |
3 ms |
728 KB |
Output is correct |
12 |
Correct |
3 ms |
728 KB |
Output is correct |
13 |
Correct |
3 ms |
728 KB |
Output is correct |
14 |
Correct |
2 ms |
728 KB |
Output is correct |
15 |
Correct |
3 ms |
728 KB |
Output is correct |
16 |
Correct |
2 ms |
728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
508 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
3 ms |
584 KB |
Output is correct |
10 |
Correct |
3 ms |
728 KB |
Output is correct |
11 |
Correct |
3 ms |
728 KB |
Output is correct |
12 |
Correct |
3 ms |
728 KB |
Output is correct |
13 |
Correct |
3 ms |
728 KB |
Output is correct |
14 |
Correct |
3 ms |
728 KB |
Output is correct |
15 |
Correct |
3 ms |
728 KB |
Output is correct |
16 |
Correct |
2 ms |
728 KB |
Output is correct |
17 |
Incorrect |
2 ms |
728 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
484 KB |
Output is correct |
6 |
Correct |
2 ms |
508 KB |
Output is correct |
7 |
Correct |
2 ms |
508 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
3 ms |
584 KB |
Output is correct |
10 |
Correct |
3 ms |
728 KB |
Output is correct |
11 |
Correct |
3 ms |
728 KB |
Output is correct |
12 |
Correct |
3 ms |
728 KB |
Output is correct |
13 |
Correct |
3 ms |
728 KB |
Output is correct |
14 |
Correct |
3 ms |
728 KB |
Output is correct |
15 |
Correct |
3 ms |
728 KB |
Output is correct |
16 |
Correct |
2 ms |
728 KB |
Output is correct |
17 |
Correct |
3 ms |
728 KB |
Output is correct |
18 |
Correct |
3 ms |
728 KB |
Output is correct |
19 |
Correct |
2 ms |
728 KB |
Output is correct |
20 |
Correct |
2 ms |
728 KB |
Output is correct |
21 |
Correct |
3 ms |
728 KB |
Output is correct |
22 |
Correct |
3 ms |
728 KB |
Output is correct |
23 |
Correct |
2 ms |
728 KB |
Output is correct |
24 |
Correct |
2 ms |
728 KB |
Output is correct |
25 |
Correct |
3 ms |
728 KB |
Output is correct |
26 |
Correct |
2 ms |
728 KB |
Output is correct |
27 |
Correct |
3 ms |
728 KB |
Output is correct |
28 |
Correct |
3 ms |
728 KB |
Output is correct |
29 |
Correct |
3 ms |
728 KB |
Output is correct |
30 |
Correct |
2 ms |
728 KB |
Output is correct |
31 |
Correct |
3 ms |
728 KB |
Output is correct |
32 |
Correct |
2 ms |
728 KB |
Output is correct |
33 |
Incorrect |
2 ms |
728 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |