Submission #683477

# Submission time Handle Problem Language Result Execution time Memory
683477 2023-01-18T13:44:21 Z nutella Parking (CEOI22_parking) C++17
0 / 100
2000 ms 13676 KB
#include <bits/stdc++.h>

using namespace std;

void process(vector<array<int, 2>> &a, int from, int to) {
    int k = a[from][0] == -1;
    int t = a[to][1] == -1;

    assert(t == 1 || a[from][k] == a[to][t + 1]);
    a[to][t] = a[from][k];
    a[from][k] = -1;
}

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;
                }
            }
        }
    }

    auto b = a;


    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;

        vector<bool> kek(m);
        for (int x : free) {
            assert(a[x][0] == a[x][1] && a[x][0] == -1);
            kek[x] = true;
        }

        for (int i = 0; i < m; ++i) {
            if (a[i][0] == a[i][1] && a[i][0] == -1) {
                while (true) {
                    cout << "keks" << endl;
                }
            }
        }
        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;
                        assert(a[x][1] != 0 && a[y][1] != 0);
                        done[i] = true;
                        any = true;
                    }
                }
            }

            if (!any) {
                break;
            }
        }
    }

    if (find(done.begin(), done.end(), false) != done.end()) {
//        if (m > 4) {
//            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;
                }
            }
        }
    }

    for (auto [x, y] : ans) {
        process(b, x, y);
    }

    for (int i = 0; i < m; ++i) {
        assert(b[i][0] == b[i][1]);
    }

    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:165:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |                 for (int j = 1; j < size(cycle); ++j) {
      |                                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Execution timed out 2093 ms 9976 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2072 ms 13676 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2072 ms 9952 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2072 ms 9952 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2063 ms 9736 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Execution timed out 2093 ms 9976 KB Time limit exceeded
4 Halted 0 ms 0 KB -