#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;}
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
vector < vi > A;
int MX,n,k;
vi One;
vector < pii > answer;
void G(int sz) {
while (A.size() <= sz)
A.pb(vi(n,0));
}
int add1(int index) {
int ok = 0;
answer.pb(mp(1,index));
int lst = -1;
for (int i = 0;i < A.size();++i)
if (A[i][index] == 1)
lst = i;
for (int cs = 0,i = lst + 1;cs < k && i <= lst + k;++cs,++i) {
G(i);
A[i][index] = 1;
if (A[i] == One) {
A.erase(A.begin() + i);
ok = 1;
--i;--lst;
}
}
return ok;
}
int add2(int index) {
int ok = 0;
answer.pb(mp(2,index));
int lst = -1;
for (int i = index;i < index + k;++i)
for (int j = 0;j < A.size();++j)
if (A[j][i] == 1)
smax(lst,j);
G(++lst);
for (int i = index;i < index + k;++i)
A[lst][i] = 1;
if (A[lst] == One)
A.erase(A.begin() + lst),ok = 1;
return ok;
}
int main(void) {
cin>>n>>k;
for (int i = 0;i < n;++i)
One.pb(1);
vi s(n);
for (auto & it : s)
cin>>it;
vi sum(k,0);
for (int i = 0;i < n;++i)
(sum[i % k] += s[i]) %= k;
int ok = 0;
for (int i = 0;i + 1 < (n % k);++i)
if (sum[i] != sum[i + 1])
return puts("-1") * 0;
for (int i = (n % k);i + 1 < k;++i)
if (sum[i] != sum[i + 1])
return puts("-1") * 0;
for (int i = 0;i < n;++i) {
G(s[i]);
for (int j = 0;j < s[i];++j)
A[j][i] = 1;
}
for (int i = 1;i < n;++i) {
while (s[i - 1] > s[i]) {
add1(i);
s[i] += k;
}
}
for (int i = 0;i < A.size();++i) {
int w = -1;
while (w + 1 < n && A[i][w + 1] == 0)
++w;
while (w >= k - 1) {
if (add2(w - k + 1))
--i;
w -= k;
}
}
while (!A.empty() && A[0][n - 1] == 1) {
for (int i = 0;!add1(i);++i) ;
}
if (!A.empty())
for (int i = 0;i < (n % k);++i)
while (!A.back()[i])
add1(i);
while (!A.empty()) {
for (int i = (n % k);i < n;i += k)
add2(i);
}
cout << (answer.size()) << '\n';
for (auto it : answer)
cout << it.fi << ' ' << (it.se + 1) << '\n';
return 0;
}
Compilation message
joiris.cpp: In function 'void G(int)':
joiris.cpp:22:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (A.size() <= sz)
~~~~~~~~~^~~~~
joiris.cpp: In function 'int add1(int)':
joiris.cpp:29:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0;i < A.size();++i)
~~^~~~~~~~~~
joiris.cpp: In function 'int add2(int)':
joiris.cpp:48:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0;j < A.size();++j)
~~^~~~~~~~~~
joiris.cpp: In function 'int main()':
joiris.cpp:86:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0;i < A.size();++i) {
~~^~~~~~~~~~
joiris.cpp:68:9: warning: unused variable 'ok' [-Wunused-variable]
int ok = 0;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
3 ms |
488 KB |
Output is correct |
6 |
Correct |
3 ms |
536 KB |
Output is correct |
7 |
Correct |
4 ms |
752 KB |
Output is correct |
8 |
Correct |
2 ms |
808 KB |
Output is correct |
9 |
Correct |
3 ms |
808 KB |
Output is correct |
10 |
Correct |
2 ms |
824 KB |
Output is correct |
11 |
Correct |
3 ms |
876 KB |
Output is correct |
12 |
Correct |
2 ms |
876 KB |
Output is correct |
13 |
Correct |
4 ms |
884 KB |
Output is correct |
14 |
Correct |
2 ms |
884 KB |
Output is correct |
15 |
Correct |
4 ms |
884 KB |
Output is correct |
16 |
Correct |
4 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
896 KB |
Output is correct |
2 |
Correct |
3 ms |
904 KB |
Output is correct |
3 |
Correct |
3 ms |
912 KB |
Output is correct |
4 |
Correct |
4 ms |
916 KB |
Output is correct |
5 |
Correct |
3 ms |
920 KB |
Output is correct |
6 |
Correct |
4 ms |
936 KB |
Output is correct |
7 |
Correct |
3 ms |
956 KB |
Output is correct |
8 |
Correct |
3 ms |
956 KB |
Output is correct |
9 |
Correct |
2 ms |
964 KB |
Output is correct |
10 |
Correct |
2 ms |
968 KB |
Output is correct |
11 |
Correct |
2 ms |
1080 KB |
Output is correct |
12 |
Correct |
3 ms |
1080 KB |
Output is correct |
13 |
Correct |
3 ms |
1080 KB |
Output is correct |
14 |
Correct |
2 ms |
1080 KB |
Output is correct |
15 |
Correct |
2 ms |
1080 KB |
Output is correct |
16 |
Correct |
2 ms |
1080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
3 ms |
488 KB |
Output is correct |
6 |
Correct |
3 ms |
536 KB |
Output is correct |
7 |
Correct |
4 ms |
752 KB |
Output is correct |
8 |
Correct |
2 ms |
808 KB |
Output is correct |
9 |
Correct |
3 ms |
808 KB |
Output is correct |
10 |
Correct |
2 ms |
824 KB |
Output is correct |
11 |
Correct |
3 ms |
876 KB |
Output is correct |
12 |
Correct |
2 ms |
876 KB |
Output is correct |
13 |
Correct |
4 ms |
884 KB |
Output is correct |
14 |
Correct |
2 ms |
884 KB |
Output is correct |
15 |
Correct |
4 ms |
884 KB |
Output is correct |
16 |
Correct |
4 ms |
896 KB |
Output is correct |
17 |
Correct |
3 ms |
1080 KB |
Output is correct |
18 |
Correct |
2 ms |
1080 KB |
Output is correct |
19 |
Execution timed out |
1073 ms |
34212 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
3 ms |
488 KB |
Output is correct |
6 |
Correct |
3 ms |
536 KB |
Output is correct |
7 |
Correct |
4 ms |
752 KB |
Output is correct |
8 |
Correct |
2 ms |
808 KB |
Output is correct |
9 |
Correct |
3 ms |
808 KB |
Output is correct |
10 |
Correct |
2 ms |
824 KB |
Output is correct |
11 |
Correct |
3 ms |
876 KB |
Output is correct |
12 |
Correct |
2 ms |
876 KB |
Output is correct |
13 |
Correct |
4 ms |
884 KB |
Output is correct |
14 |
Correct |
2 ms |
884 KB |
Output is correct |
15 |
Correct |
4 ms |
884 KB |
Output is correct |
16 |
Correct |
4 ms |
896 KB |
Output is correct |
17 |
Correct |
2 ms |
896 KB |
Output is correct |
18 |
Correct |
3 ms |
904 KB |
Output is correct |
19 |
Correct |
3 ms |
912 KB |
Output is correct |
20 |
Correct |
4 ms |
916 KB |
Output is correct |
21 |
Correct |
3 ms |
920 KB |
Output is correct |
22 |
Correct |
4 ms |
936 KB |
Output is correct |
23 |
Correct |
3 ms |
956 KB |
Output is correct |
24 |
Correct |
3 ms |
956 KB |
Output is correct |
25 |
Correct |
2 ms |
964 KB |
Output is correct |
26 |
Correct |
2 ms |
968 KB |
Output is correct |
27 |
Correct |
2 ms |
1080 KB |
Output is correct |
28 |
Correct |
3 ms |
1080 KB |
Output is correct |
29 |
Correct |
3 ms |
1080 KB |
Output is correct |
30 |
Correct |
2 ms |
1080 KB |
Output is correct |
31 |
Correct |
2 ms |
1080 KB |
Output is correct |
32 |
Correct |
2 ms |
1080 KB |
Output is correct |
33 |
Correct |
3 ms |
1080 KB |
Output is correct |
34 |
Correct |
2 ms |
1080 KB |
Output is correct |
35 |
Execution timed out |
1073 ms |
34212 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |