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;
typedef long long lint;
typedef pair<int, int> pii;
inline lint _min(lint x, lint y){ return x<y ? x : y; }
inline lint _max(lint x, lint y){ return x>y ? x : y; }
inline lint _abs(lint x){ return x<0 ? -x : x; }
int n, k, A[64];
vector<pii> ans;
void clear(int l, int r){
for(int i=l; i<=r; i++) A[i]--;
}
void up(int i){
A[i]+=k; ans.push_back({1, i});
}
void debug(){
return;
cout<<"Debug:\n";
for(int i=1; i<=n; i++) cout<<A[i]<<' ';
cout<<"\n\n";
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int C[64] = {};
cin>>n>>k;
for(int i=1; i<=n; i++) cin>>A[i], C[(i-1)%k]+=A[i];
for(int i=0; i<n%k; i++) if(C[i]%k != C[0]%k){
cout<<"-1\n"; return 0;
}
for(int i=n%k; i<k; i++) if(C[i]%k != C[k-1]%k){
cout<<"-1\n"; return 0;
}
for(int i=2; i<=n; i++){
while(A[i]<A[i-1]) up(i);
}
while(A[1]) clear(1, n);
debug();
for(int i=1; i<=k; i++){
for(int j=i+k; j<=n; j+=k){
int x = A[j]-A[j-1];
for(int b=0; b<x; b++){
for(int a=i; a<j; a+=k){
ans.push_back({2, a});
}
clear(j, n); clear(1, i-1);
}
}
while(A[i]<A[n]) up(i);
while(A[i+1]) clear(1, n);
}
debug();
{
int mx = *max_element(A+1, A+k+1);
for(int i=1; i<=n%k; i++){
while(A[i]<mx) up(i);
}
}
{
int mx = *max_element(A+n%k+1, A+k+1);
for(int i=n%k+1; i<=k; i++){
while(A[i]<mx) up(i);
}
}
for(int b=A[k]; b<A[1]; b++) ans.push_back({2, n%k+1});
for(int a=n%k+1+k; a<=n; a+=k) for(int b=0; b<A[1]; b++) ans.push_back({2, a});
for(int b=A[1]; b>A[k]; b--) clear(1, n%k);
debug();
for(int b=A[1]; b%k!=0; b++) ans.push_back({2, 1});
for(int b=A[1]; b%k!=0; b++) for(int a=n%k+1+k; a<=n; a+=k) ans.push_back({2, 1});
for(int b=0; b<A[1]; b+=k) for(int a=k+1; a<n%k+1+k; a++) ans.push_back({1, a});
for(int b=A[1]; b>0; b--) clear(1, k);
debug();
cout<<ans.size()<<'\n';
for(auto [a, b]: ans){
cout<<a<<' '<<b<<'\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... |