# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
804124 |
2023-08-03T07:16:25 Z |
이동현(#10103) |
Shifty Grid (CCO17_shifty) |
C++17 |
|
1 ms |
512 KB |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
assert(n == 2 && m == 2);
vector<vector<int>> a(n, vector<int>(m));
vector<array<int, 2>> pos(n * m);
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cin >> a[i][j];
pos[a[i][j]] = {i, j};
}
}
vector<vector<int>> ans;
auto rotr = [&](int row)->void{
int imsi = a[row][m - 1];
for(int i = m - 1; i > 0; --i){
a[row][i] = a[row][i - 1];
}
a[row][0] = imsi;
for(int i = 0; i < m; ++i){
pos[a[row][i]] = {row, i};
}
};
auto rotrt = [&](int now, int t)->void{
if(t) ans.push_back({0, now, t});
while(t--) rotr(now);
};
auto rotc = [&](int col)->void{
int imsi = a[n - 1][col];
for(int i = n - 1; i > 0; --i){
a[i][col] = a[i - 1][col];
}
a[0][col] = imsi;
for(int i = 0; i < n; ++i){
pos[a[i][col]] = {i, col};
}
};
auto rotct = [&](int now, int t)->void{
if(t) ans.push_back({1, now, t});
while(t--) rotc(now);
};
auto toleft = [&](array<int, 2> pos, int torow){
rotrt(pos[0], m - pos[1]);
rotct(0, 1);
rotrt(pos[0], pos[1]);
rotct(0, (torow - pos[0] - 1 + n * 2) % n);
};
auto output = [&](){
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cout << a[i][j] << ' ';
}
cout << '\n';
}
cout << endl;
};
for(int i = n - 1; i >= 0; --i){
for(int j = 1; j < m; ++j){
toleft(pos[i * m + m - j], i);
rotrt(i, 1);
// output();
}
}
for(int i = n - 1; i > 0; --i){
int id = i * m;
if(pos[id][1] == 0){
int sv = pos[id][0];
rotct(0, n - sv);
rotrt(0, m - 1);
rotct(0, (sv - i + n) % n);
rotrt(0, 1);
rotct(0, i);
}
else{
rotct(0, n - i);
rotrt(0, m - 1);
rotct(0, i);
rotrt(0, 1);
}
// output();
// cout << "HI" << endl;
}
auto ok = [&](){
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(a[i][j] != i * m + j){
return 0;
}
}
}
return 1;
};
while(!ok()){
rotct(0, 1);
if(ok()) break;
rotrt(0, m - 1);
if(ok()) break;
rotct(0, 1);
if(ok()) break;
rotrt(0, 1);
if(ok()) break;
}
assert((int)ans.size() <= 100000);
cout << (int)ans.size() << '\n';
for(auto&i:ans){
cout << i[0] + 1 << ' ' << i[1] + 1 << ' ' << i[2] << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:65:10: warning: variable 'output' set but not used [-Wunused-but-set-variable]
65 | auto output = [&](){
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |