Submission #683447

# Submission time Handle Problem Language Result Execution time Memory
683447 2023-01-18T12:10:51 Z nutella Parking (CEOI22_parking) C++17
0 / 100
55 ms 4772 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<array<int, 2>> a(m), inv(n, {-1, -1}), ans;

    auto drivable = [&](int i, int p) {
        assert(i < n && p < 2);
        int pos = inv[i][p];
        if (pos == -1) {
            while (true) {
                cout << "axaxa" << endl;
            }
        }

        return a[pos][0] == i || a[pos][0] == -1;
    };

    for (int i = 0; i < m; ++i) {
        cin >> a[i][1] >> a[i][0];
        --a[i][1], --a[i][0];

        for (int k : a[i]) {
            if (k != -1) {
                if (inv[k][0] == -1) {
                    inv[k][0] = i;
                } else {
                    inv[k][1] = i;
                }
            }
        }
    }

    vector<bool> done(n);

    vector<int> free;
    for (int i = 0; i < m; ++i) {
        if (a[i][0] == -1 && a[i][1] == -1) {
            free.push_back(i);
        }
    }

    for (int i = 0; i < n; ++i) {
        if (inv[i][0] == inv[i][1]) {
            done[i] = true;
        }
    }

    while (find(done.begin(), done.end(), false) != done.end()) {
        bool any = false;
        for (int i = 0; i < n; ++i) {
            if (!done[i]) {
                int from = inv[i][0], to = inv[i][1];
                if (drivable(i, 0) && drivable(i, 1) && (a[from][0] == -1 || a[to][0] == -1)) {
                    if (a[to][0] != -1) {
                        swap(from, to);
                    }

                    done[i] = true;
                    any = true;

                    ans.push_back({from, to});

                    a[from][(a[from][1] == i)] = -1;
                    a[to][0] = i;
                    inv[i][0] = inv[i][1] = to;

                    if (a[from][1] == -1) {
                        free.push_back(from);
                    }
                }
            }
        }

        if (!any) {
            for (int i = 0; i < n; ++i) {
                if (!done[i]) {
                    int x = inv[i][0], y = inv[i][1];
                    if (drivable(i, 0) && drivable(i, 1) && !free.empty()) {
                        ans.push_back({x, free.back()});
                        ans.push_back({y, free.back()});

                        inv[i][0] = inv[i][1] = free.back();
                        free.pop_back();

                        a[x][0] = -1, a[y][0] = -1;
                        done[i] = true;
                        any = true;
                    }
                }
            }

            if (!any) {
                break;
            }
        }
    }

    if (m > 4 && find(done.begin(), done.end(), false) != done.end()) {
        assert(false);
        if (free.empty()) {
            cout << "-1\n";
            return 0;
        }

        for (int i = 0; i < n; ++i) {
            if (!done[i]) {
                int &x = inv[i][0], &y = inv[i][1];
                if (a[x][0] != i) {
                    swap(x, y);
                }
                assert(a[inv[i][0]][0] == i && a[inv[i][1]][1] == i);
            }
        }

        for (int i = 0; i < n; ++i) {
            if (!done[i]) {
                int x = inv[i][0];
                int y = inv[i][0];

                vector<int> cycle;
                do {
                    cycle.push_back(y);
                    y = inv[a[y][1]][0];
                } while (x != y);

                ans.push_back({cycle[0], free.back()});
                for (int j = 1; j < size(cycle); ++j) {
                    ans.push_back({cycle[j], cycle[j - 1]});
                }
                ans.push_back({free.back(), cycle.back()});

                for (int j : cycle) {
                    done[a[j][0]] = done[a[j][1]] = true;
                }
            }
        }
    }


    cout << ans.size() << '\n';
    for (auto [x, y] : ans) {
        cout << x + 1 << " " << y + 1 << '\n';
    }

    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:135:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |                 for (int j = 1; j < size(cycle); ++j) {
      |                                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 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 Incorrect 0 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 55 ms 4772 KB Output isn't correct
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 -
# 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 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 Incorrect 0 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -