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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_set>
#include <bitset>
#include <time.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb push_back
#define szz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
typedef tuple<int, int, int> t3;
int N, K;
int A[55];
ll X[5050];
int H[55];
ll FULL;
vector <pii> ans;
void erase(int x) {
int mxh = -1;
for(int i=1;i<=N;i++) mxh = max(mxh, H[i]);
for(int i=x;i<mxh;i++) X[i] = X[i+1];
X[mxh] = 0;
}
void fix_h() {
for(int i=1;i<=N;i++) {
while(H[i] > 0 && !(X[H[i]] & 1LL << i)) --H[i];
}
}
void put(int vert, int x) {
ans.pb(pii(vert, x));
if(vert == 2) {
for(int i=x;i<x+K;i++) A[i] += 1;
int mx = -1;
for(int i=x;i<x+K;i++) mx = max(mx, H[i]);
for(int i=x;i<x+K;i++) H[i] = mx + 1;
X[mx + 1] += 1LL << (x + K);
X[mx + 1] -= 1LL << x;
if(X[mx + 1] == FULL) {
erase(mx + 1);
fix_h();
}
}
else {
A[x] += K;
int h = H[x];
for(int i=h+1;i<=h+K;i++) X[i] |= 1LL << x;
H[x] += K;
for(int a=0, i=h+1;a<K;a++) {
if(X[i] == FULL) {
erase(i);
fix_h();
}
else ++i;
}
}
}
int gc(int x, int y) { return y == 0 ? x : gc(y, x%y); }
int main() {
scanf("%d%d", &N, &K);
FULL = (1LL << (N + 1)) - 2;
for(int i=1;i<=N;i++) scanf("%d", A + i);
for(int i=1;i<=N;i++) {
H[i] = A[i];
for(int j=1;j<=A[i];j++) X[j] |= 1LL << i;
}
int sum[55] = {};
int g = gc(N, K);
for(int i=1;i<=N;i++) sum[i % g] += A[i];
for(int i=1;i<g;i++) if(sum[0] % K != sum[i] % K) {
puts("-1");
return 0;
}
for(int i=N-1;i;i--) while(A[i] < A[i+1]) {
put(1, i);
}
int tsum = 0;
for(int i=1;i<=N;i++) tsum += A[i];
int tar = -1;
for(int i=A[1];;i++) if(N * i % K == tsum % K) {
tar = i; break;
}
for(int i=1;i+K-1<=N;i++) {
while(A[i] < tar) put(2, i);
}
for(int i=N-K+2;i<=N;i++) {
int delta = (tar - A[i]);
if(delta % K != 0) {
puts("-1");
return 0;
}
while(A[i] < tar) put(1, i);
}
while(1) {
int mxh = -1;
for(int i=1;i<=N;i++) mxh = max(mxh, H[i]);
if(mxh == 0) break;
int f = -1;
for(int i=1;i<=N;i++) if(!(1LL<<i & X[mxh])) {
if(f == -1 || H[f] > H[i]) f = i;
}
put(1, f);
}
printf("%d\n", szz(ans));
for(pii e : ans) printf("%d %d\n", e.Fi, e.Se);
return 0;
}
Compilation message (stderr)
joiris.cpp: In function 'int main()':
joiris.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
joiris.cpp:88:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=N;i++) scanf("%d", A + 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... |