// In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define debug(x) cerr << #x << " : " << x << "\n"
#define use_file freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
using namespace std;
typedef int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef string str;
const ll N = 105, MsK = 2e4, mod = 1e9 + 7;
ll n, k, a[N], b[N];
bool mrk[1505][55], mrk2[1505][55];
vector <pll> vec;
bool f(ll i){
for(ll j = 0; j < n; j ++)if(!mrk2[i][j])return 0;
return 1;
}
void update(){
for(ll i = 0; i <= 1504; i ++){
for(ll j = 0; j < n; j ++)mrk2[i][j] = mrk[i][j];
}
ll I = 1504;
for(ll i = 1504; i >= 0; i --){
if(f(i))continue;
for(ll j = 0; j < n; j ++)mrk[I][j] = mrk2[i][j];
I --;
}
while(I >= 0){
for(ll j = 0; j < n; j ++)mrk[I][j] = 0;
I --;
}
}
bool f2(){
for(ll i = (n % k); i < k - 1; i ++)if(mrk[1504][i])return 1;
return 0;
}
bool f3(){
for(ll i = 0; i < (n % k); i ++)if(mrk[1504][i])return 1;
return 0;
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> k;
for(ll i = 0; i < n; i ++){
cin >> a[i];
b[i % k] = (b[i % k] + a[i]) % k;
if(i > 0){
while(a[i] < a[i - 1]){
vec.pb({1, i + 1});
a[i] += k;
}
}
}
ll z = ((n % k) - 1 + k) % k;
for(ll i = 1; i <= z; i ++){
if(b[i] != b[i - 1])return cout << -1 << endl, 0;
}
z = (n % k);
for(ll i = z + 1; i < k; i ++){
if(b[i] != b[i - 1])return cout << -1 << endl, 0;
}
for(ll i = n - 1; i >= 0; i --)a[i] -= a[0];
ll Y = 1504;
for(ll i = 1; i < n; i ++){
for(ll j = 0; j < a[i]; j ++)mrk[Y - j][i] = 1;
}
for(ll i = Y; i > Y - a[n - 1]; i --){
for(ll j = n - 1; j > k - 2; j --){
if(!mrk[i][j]){
vec.pb({2, j - k + 2});
for(ll h = j; h > j - k; h --)mrk[i][h] = 1;
j -= k - 1;
}
}
}
update();
while(mrk[Y][n - 1]){
a[n - 1] = 0;
ll y = Y;
while(mrk[y][n - 1]){
y --;
a[n - 1] ++;
}
for(ll i = 0; i < k - 1; i ++){
ll yy = Y + 1;
for(ll j = 0; j <= Y; j ++){
if(mrk[j][i]){
yy = j;
break;
}
}
while(yy > Y - a[n - 1] + 1){
vec.pb({1, i + 1});
for(ll h = yy - 1; h >= yy - k; h --)mrk[h][i] = 1;
yy -= k;
}
}
update();
}
while((n % k) < k - 1 && f2()){
ll mx = 0;
for(ll i = (n % k); i < k - 1; i ++){
ll y = Y;
ll x = 0;
while(mrk[y][i]){
x ++;
y --;
}
mx = max(mx, x);
}
for(ll i = 0; i < n; i ++){
ll x = 0;
ll y = Y;
while(mrk[y][i]){
x ++;
y --;
}
while(x < mx){
for(ll j = Y - x; j > Y - x - k; j --)mrk[j][i] = 1;
x += k;
vec.pb({1, i + 1});
}
}
update();
}
while((n % k) - 1 >= 0 && f3()){
ll mx = 0;
for(ll i = 0; i < (n % k); i ++){
ll y = Y;
ll x = 0;
while(mrk[y][i]){
x ++;
y --;
}
mx = max(mx, x);
}
for(ll i = 0; i < (n % k); i ++){
ll x = 0;
ll y = Y;
while(mrk[y][i]){
x ++;
y --;
}
while(x < mx){
x += k;
vec.pb({1, i + 1});
}
for(ll j = Y - (mx - x); j > Y - mx; j --)mrk[j][i] = 0;
for(ll j = Y; j > Y - (mx - x); j --)mrk[j][i] = 1;
}
ll x = (n % k);
while(x < n){
for(ll j = Y; j > Y - mx; j --)vec.pb({2, x + 1});
x += k;
}
}
cout << vec.size() << "\n";
for(auto i : vec)cout << i.F << ' ' << i.S << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Execution timed out |
1096 ms |
492 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
620 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
0 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Execution timed out |
1096 ms |
492 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |