QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#261998#1646. Disk Sortucup-team133#RE 0ms3628kbC++173.5kb2023-11-23 14:35:542023-11-23 14:35:55

Judging History

你现在查看的是最新测评结果

  • [2023-11-23 14:35:55]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3628kb
  • [2023-11-23 14:35:54]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<pair<int, int>> ans;
    vector<vector<int>> a(n + 1, vector<int>(3));
    auto Move = [&](int x, int y) {
        if (x == y) {
            return;
        }
        ans.emplace_back(x + 1, y + 1);
        assert((int) ans.size() <= 6 * n);
        assert(a[y][0] == 0);
        for (int i = 0; i < 3; i++) {
            if (a[x][i] != 0) {
                swap(a[x][i], a[y][0]);
                break;
            } else if (i == 2) {
                assert(false);
            }
        }
        for (int i = 0; i < 2; i++) {
            if (a[y][i + 1] == 0) {
                swap(a[y][i], a[y][i + 1]);
            }
        }
    };
    auto Top = [&](int x) {
        for (int i = 0; i < 3; i++) {
            if (a[x][i] != 0) {
                return a[x][i];
            }
        }
        return 0;
    };
    for (int j = 0; j < 3; j++) {
        for (int i = 0; i < n; i++) {
            cin >> a[i][j];
        }
    }
    while (true) {
        vector<vector<int>> t(n + 1);
        int z = -1;
        for (int i = 0; i < n + 1; i++) {
            if (a[i] == vector<int>(3, a[i][0])) {
                if (a[i][0] == 0) {
                    z = i;
                }
                continue;
            }
            for (int j = 0; j < 3; j++) {
                t[a[i][j]].emplace_back(j);
            }
        }
        int c = -1;
        for (int i = 0; i < n + 1; i++) {
            if (t[i].empty()) {
                continue;
            }
            sort(t[i].begin(), t[i].end());
            if (c == -1 || t[c] > t[i]) {
                c = i;
            }
        }
        if (c == -1) {
            break;
        }
        assert(t[c][0] == 0);
        assert(t[c][1] <= 1);
        vector<pair<int, int>> s;
        for (int i = 0; i < n + 1; i++) {
            for (int j = 0; j < 3; j++) {
                if (a[i][j] == c) {
                    s.emplace_back(j, i);
                }
            }
        }
        sort(s.begin(), s.end());
//        for (int i = 0; i < 3; i++) {
//            cerr << s[i].first << " " << s[i].second << endl;
//        }
        for (int i = 0; i < 3; i++) {
//            cerr << Top(s[i].second) << " " << c << endl;
            while (Top(s[i].second) != c) {
                assert(i != 0);
                for (int j = i - 1; j >= 0; j--) {
                    if (a[s[j].second][0] == 0) {
                        Move(s[i].second, s[j].second);
                    } else {
                        assert(j != 0);
                    }
                }
            }
            Move(s[i].second, z);
        }
        while (a[s[2].second][2] != 0) {
            for (int j = 1; j >= 0; j--) {
                if (a[s[j].second][0] == 0) {
                    Move(s[2].second, s[j].second);
                    break;
                } else {
                    assert(j != 0);
                }
            }
        }
    }
    if (a[n][0] != 0) {
        for (int i = 0; i < n; i++) {
            if (a[i][0] == 0) {
                Move(n, i);
                Move(n, i);
                Move(n, i);
                break;
            }
        }
    }
//    for (int i = 0; i < n + 1; i++) {
//        for (int j = 0; j < 3; j++) {
//            cerr << a[i][j] << " \n"[j == 2];
//        }
//    }
    cout << ans.size() << '\n';
    for (auto [x, y]: ans) {
        cout << x << " " << y << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

input:

4
2 3 1 4
2 1 1 4
2 3 3 4

output:

9
3 5
2 3
2 5
3 2
3 5
3 2
5 3
5 3
5 3

result:

ok n=4

Test #2:

score: 0
Accepted
time: 0ms
memory: 3532kb

input:

2
1 2
1 2
1 2

output:

0

result:

ok n=2

Test #3:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

3
2 2 1
1 3 3
1 2 3

output:

11
1 4
2 4
2 1
2 4
3 2
1 3
1 2
1 2
4 1
4 1
4 1

result:

ok n=3

Test #4:

score: -100
Runtime Error

input:

3
2 1 3
2 1 1
2 3 3

output:


result: