#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
int a[101][101], b[101][101];
pair<int, int> pos[100100];
vector<array<int, 3>> Q;
void shiftR(int i, int c){
assert(0 <= c && c < m);
for (int j=0;j<m;j++) b[i][(j+c)%m] = a[i][j];
for (int j=0;j<m;j++){
a[i][j] = b[i][j];
pos[a[i][j]] = {i, j};
}
Q.push_back({1, i+1, c});
if (Q.size() >= 100000) exit(0);
}
void shiftC(int j, int c){
assert(0 <= c && c < n);
for (int i=0;i<n;i++) b[(i+c)%n][j] = a[i][j];
for (int i=0;i<n;i++){
a[i][j] = b[i][j];
pos[a[i][j]] = {i, j};
}
Q.push_back({2, j+1, c});
if (Q.size() >= 100000) exit(0);
}
void solveR(int p){
for (int j=0;j<m;j++){
auto [cx, cy] = pos[p*m + j];
if (cx==p && cy==j) continue;
if (cx==p){
shiftC(j, 1);
shiftC(cy, 1);
shiftR(p+1, (m+j-cy) % m);
shiftC(j, n-1);
shiftC(cy, n-1);
}
else if (cy==j){
shiftR(cx, 1);
shiftC(j, (n+cx-p) % n);
shiftR(cx, m-1);
shiftC(j, (n+p-cx) % n);
}
else{
shiftC(j, (n+cx-p) % n);
shiftR(cx, (m+j-cy) % m);
shiftC(j, (n+p-cx) % n);
}
}
// printf("ok\n");
// for (int i=0;i<n;i++){
// for (int j=0;j<m;j++) printf("%d ", a[i][j]);
// printf("\n");
// }
}
void swap2(int p, int q){
shiftC(p, 1);
shiftR(0, (m+q-p) % m);
shiftC(q, 1);
shiftR(0, (m+p-q) % m);
shiftC(p, 1);
}
void sort2(){
for (int j=0;j<m;j++){
auto [cx, cy] = pos[m+j];
if (cy==j) continue;
swap2(j, cy);
}
}
void swapn(int p, int q){
for (int i=0;i<n;i++){
if (i%2==0){
shiftC(q, n-1);
shiftR(n-1, (m+q-p) % m);
}
else{
shiftC(q, n-1);
shiftR(n-1, (m+p-q) % m);
}
}
shiftC(q, n-1);
}
void sortn(){
for (int j=0;j<m;j++){
auto [cx, cy] = pos[(n-1)*m+j];
if (cy==j) continue;
swapn(j, cy);
}
}
void solve(){
Q.clear();
for (int i=0;i<n;i++){
for (int j=0;j<m;j++) pos[a[i][j]] = {i, j};
}
for (int i=0;i<n-1;i++) solveR(i);
if (m==2){
if (a[n-1][0] > a[n-1][1]) shiftR(n-1, 1);
}
else if (n==2) sort2();
else sortn();
}
mt19937 seed(1557);
uniform_int_distribution<int> rng(0, 2147483647);
int getrand(int l, int r){return rng(seed)%(r-l+1) + l;}
vector<array<int, 3>> X;
void gen(){
n = getrand(1, 10)*2, m = getrand(1, 10)*2;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++) a[i][j] = i*m+j;
}
X.clear();
Q.clear();
for (int z=0;z<20;z++){
int typ = getrand(0, 1);
if (typ==0){
shiftR(getrand(0, n-1), getrand(0, m-1));
}
else{
shiftC(getrand(0, m-1), getrand(0, n-1));
}
}
X = Q;
}
set<vector<int>> st;
int stress(int tc){
printf("----------------------------------\n");
printf("Stress #%d\n", tc);
gen();
printf("Input:\n");
printf("%d %d\n", n, m);
for (int i=0;i<n;i++){
for (int j=0;j<m;j++) printf("%d ", a[i][j]);
printf("\n");
}
printf("\n");
solve();
for (int i=0;i<n;i++){
for (int j=0;j<m;j++) if (a[i][j]!=i*m+j){
printf("Failed:\n");
for (int i=0;i<n;i++){
for (int j=0;j<m;j++) printf("%d ", a[i][j]);
printf("\n");
}
// vector<int> V;
// for (int j=0;j<m;j++) V.push_back(a[i][j]);
// for (int j=0;j<m-1;j++){
// swap(a[i][j], a[i][j+1]);
// if (is_sorted(a[i], a[i]+m)) return 1;
// swap(a[i][j], a[i][j+1]);
// }
// st.insert(V);
assert(0);
}
}
printf("Accepted: %d\n", (int)Q.size());
return 0;
}
void simulate(int n, int m, const vector<array<int, 3>> &Q){
for (int i=0;i<n;i++){
for (int j=0;j<m;j++) a[i][j] = i*m + j;
}
for (auto &[x, y, z]:Q){
if (x==1) shiftR(y-1, z);
else shiftC(y-1, z);
printf("------------------------\n");
printf("%d %d %d\n", x, y, z);
for (int i=0;i<n;i++){
for (int j=0;j<m;j++) printf("%2d ", a[i][j]);
printf("\n");
}
}
printf("ok %d\n", (int)Q.size());
}
int main(){
// for (int i=1;i<=100;i++) stress(i);
// int cnt = 1;
// while(true){
// if (stress(cnt++)) break;
// }
// printf("ok %d + %d generated\n", (int)X.size(), (int)Q.size());
// for (auto &x:Q) X.push_back(x);
// simulate(6, 6, X);
// for (auto &V:st){
// for (auto &x:V) printf("%d ", x);
// printf("\n");
// }
scanf("%d %d", &n, &m);
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
scanf("%d", a[i]+j);
pos[a[i][j]] = {i, j};
}
}
solve();
// printf("ok done\n");
// for (int i=0;i<n;i++){
// for (int j=0;j<m;j++) printf("%d ", a[i][j]);
// printf("\n");
// }
printf("%d\n", (int)Q.size());
for (auto &[x, y, z]:Q) printf("%d %d %d\n", x, y, z);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:232:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
232 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:235:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
235 | scanf("%d", a[i]+j);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
22 ms |
1104 KB |
Output is correct |
7 |
Correct |
28 ms |
1104 KB |
Output is correct |
8 |
Correct |
21 ms |
1080 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
22 ms |
1104 KB |
Output is correct |
19 |
Correct |
28 ms |
1104 KB |
Output is correct |
20 |
Correct |
21 ms |
1080 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
15 ms |
976 KB |
Output is correct |
23 |
Correct |
51 ms |
1680 KB |
Output is correct |
24 |
Correct |
39 ms |
1436 KB |
Output is correct |
25 |
Correct |
33 ms |
1380 KB |
Output is correct |
26 |
Correct |
23 ms |
1104 KB |
Output is correct |
27 |
Correct |
41 ms |
1476 KB |
Output is correct |
28 |
Correct |
41 ms |
1524 KB |
Output is correct |
29 |
Correct |
40 ms |
1488 KB |
Output is correct |
30 |
Correct |
42 ms |
1496 KB |
Output is correct |
31 |
Correct |
42 ms |
1564 KB |
Output is correct |
32 |
Correct |
40 ms |
1492 KB |
Output is correct |
33 |
Correct |
6 ms |
660 KB |
Output is correct |
34 |
Correct |
5 ms |
660 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
24 ms |
1312 KB |
Output is correct |
37 |
Correct |
5 ms |
468 KB |
Output is correct |
38 |
Correct |
5 ms |
660 KB |
Output is correct |
39 |
Correct |
6 ms |
688 KB |
Output is correct |
40 |
Correct |
12 ms |
908 KB |
Output is correct |
41 |
Correct |
5 ms |
628 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
21 ms |
1084 KB |
Output is correct |
44 |
Correct |
3 ms |
480 KB |
Output is correct |
45 |
Correct |
15 ms |
976 KB |
Output is correct |
46 |
Correct |
8 ms |
788 KB |
Output is correct |