답안 #804149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
804149 2023-08-03T07:22:04 Z 이동현(#10103) Shifty Grid (CCO17_shifty) C++17
25 / 25
275 ms 3680 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;
    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{
        t %= m;
        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{
        t %= n;
        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';
        if(i[0] == 0) assert(i[1] >= 0 && i[1] < n && i[2] >= 0 && i[2] < m);
        else if(i[0] == 1) assert(i[1] >= 0 && i[1] < m && i[2] >= 0 && i[2] < n);
        else assert(0);
    }
    
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:66:10: warning: variable 'output' set but not used [-Wunused-but-set-variable]
   66 |     auto output = [&](){
      |          ^~~~~~
# 결과 실행 시간 메모리 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 0 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 0 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 0 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 0 ms 212 KB Output is correct
5 Correct 239 ms 3580 KB Output is correct
6 Correct 227 ms 3660 KB Output is correct
7 Correct 244 ms 3640 KB Output is correct
8 Correct 235 ms 3568 KB Output is correct
9 Correct 232 ms 3640 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 0 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 0 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 0 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 0 ms 212 KB Output is correct
17 Correct 239 ms 3580 KB Output is correct
18 Correct 227 ms 3660 KB Output is correct
19 Correct 244 ms 3640 KB Output is correct
20 Correct 235 ms 3568 KB Output is correct
21 Correct 232 ms 3640 KB Output is correct
22 Correct 229 ms 3580 KB Output is correct
23 Correct 234 ms 3544 KB Output is correct
24 Correct 238 ms 3600 KB Output is correct
25 Correct 234 ms 3540 KB Output is correct
26 Correct 275 ms 3560 KB Output is correct
27 Correct 229 ms 3680 KB Output is correct
28 Correct 127 ms 3548 KB Output is correct
29 Correct 131 ms 3576 KB Output is correct
30 Correct 121 ms 3588 KB Output is correct
31 Correct 134 ms 3540 KB Output is correct
32 Correct 126 ms 3484 KB Output is correct
33 Correct 9 ms 944 KB Output is correct
34 Correct 11 ms 980 KB Output is correct
35 Correct 3 ms 340 KB Output is correct
36 Correct 67 ms 3028 KB Output is correct
37 Correct 15 ms 724 KB Output is correct
38 Correct 8 ms 980 KB Output is correct
39 Correct 9 ms 980 KB Output is correct
40 Correct 24 ms 1680 KB Output is correct
41 Correct 13 ms 980 KB Output is correct
42 Correct 6 ms 836 KB Output is correct
43 Correct 57 ms 2324 KB Output is correct
44 Correct 6 ms 724 KB Output is correct
45 Correct 32 ms 1796 KB Output is correct
46 Correct 15 ms 1244 KB Output is correct