#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
int n,k;
int arr[50];
int arr2[50];
int seg_cnt[50];
vector<pii> ans;
void add_hor(int x)
{
ans.pb({2,x});
rep2(i,x,x+k-1) arr[i]++;
}
void add_vert(int x)
{
ans.pb({1,x});
arr[x] += k;
}
void del_layer()
{
rep(i,n) arr[i]--;
}
void norm()
{
int max_ = 0;
rep(i,n) max_ = max(max_,arr[i]);
rep(i,n)
{
while(arr[i]/k != max_/k) add_vert(i);
}
int min_ = 1e9;
rep(i,n) min_ = min(min_,arr[i]);
rep(i,min_) del_layer();
}
bool check(int d)
{
rep(i,n) arr2[i] = arr[i];
seg_cnt[0] = d;
rep(i,k) arr2[i]+=d;
rep2(i,1,n-k)
{
seg_cnt[i] = (arr2[0]-arr2[i]+k*(ll)1e9)%k;
rep2(j,i,i+k-1) arr2[j] += seg_cnt[i];
}
bool is = 1;
rep(i,n) if(arr2[i]%k != arr2[0]%k) is = 0;
return is;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
cin >> n >> k;
rep(i,n) cin >> arr[i];
//cout << n << " " << k << "\n";
//rep(i,n) cout << arr[i] << "\n";
bool is = 0;
rep(i,k)
{
if(check(i))
{
is = 1;
break;
}
}
if(!is)
{
cout << "-1\n";
return 0;
}
norm();
rep(i,n-k+1)
{
if(seg_cnt[i] == 0) continue;
int max_ = 0;
rep2(j,i,i+k-1) max_ = max(max_,arr[j]);
rep(j,seg_cnt[i]) add_hor(i);
rep(j,n)
{
if(j >= i && j <= i+k-1) continue;
while(arr[j] < max_+seg_cnt[i]) add_vert(j);
}
rep(j,seg_cnt[i]) del_layer();
norm();
}
cout << siz(ans) << "\n";
forall(it,ans) cout << it.ff << " " << it.ss+1 << "\n";
}
| # | 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... |