Submission #762252

# Submission time Handle Problem Language Result Execution time Memory
762252 2023-06-21T07:03:57 Z resting Parking (CEOI22_parking) C++17
100 / 100
541 ms 33292 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mx = 1e5 + 5;
void solve() {
    int n, m; cin >> n >> m;
    vector<vector<int>> v(m, vector<int>(2, 0));
    vector<vector<int>> pos(n);
    vector<int> empty;
    vector<pair<int, int>> res;
    for (int i = 0; i < m; i++) {
        auto& x = v[i];
        cin >> x[0] >> x[1]; x[0]--; x[1]--;
        if (x[0] + 1) pos[x[0]].push_back(i);
        if (x[1] + 1) pos[x[1]].push_back(i);
    }

    vector<int> vis(n, 0);

    auto mov = [&](int x, int y) -> void {
        res.push_back({ x, y });
        if (v[x][1] + 1) {
            if (v[y][0] + 1) swap(v[x][1], v[y][1]);
            else swap(v[x][1], v[y][0]);
        } else {
            if (v[y][0] + 1) swap(v[x][0], v[y][1]);
            else swap(v[x][0], v[y][0]);
        }
    };



    //case 1: trivial case
    auto good = [&](int x) -> bool {
        for (int i = 0; i < 2; i++) {
            if (v[pos[x][0]][1] == -1 && (v[pos[x][1]][1] == x || v[pos[x][1]][1] == -1)) {
                return true;
            }
            swap(pos[x][0], pos[x][1]);
        }
        return false;
    };


    vector<int> q;
    for (int i = 0; i < n; i++) {
        if (pos[i][0] == pos[i][1]) {
            vis[i] = 1;
            continue;
        }
        if (good(i)) {
            q.push_back(i); vis[i] = 1;
        }
    }

    while (!q.empty()) {
        auto x = q.back();
        q.pop_back();
        // cout << "good: " << x << endl;
        mov(pos[x][1], pos[x][0]);
        auto t = v[pos[x][1]][0];
        if (t != -1 && good(t)) { vis[t] = 1; q.push_back(t); }
    }
    for (int i = 0; i < m; i++) {
        auto& x = v[i];
        if (x[0] + x[1] == -2) empty.push_back(i);
    }
    vector<int> todo;

    bool bad = 0;

    auto clear = [&](int t) {
        if (t == -1) return;
        for (int j = 0; j < 2; j++) {
            // cout << t << "," << pos[t][0] << "," << v[pos[t][0]][1] << endl;
            if (v[pos[t][0]][1] == -1) {
                while (true) {
                    //cout << "t: " << t << endl;
                    if (t != -1) vis[t] = 1;
                    if (t == -1) {
                        //process
                        reverse(todo.begin(), todo.end());
                        empty.push_back(pos[todo.back()][0]);
                        for (auto& x : todo) {
                            mov(pos[x][0], pos[x][1]);
                        }
                        todo.clear();
                        return;
                    } else if (v[pos[t][1]][1] == t) {
                        if (v[pos[t][0]][1] == t) {
                            if (!empty.size()) { cout << -1 << endl;bad = 1;  return; }
                            mov(pos[t][0], empty.back());
                            mov(pos[t][1], empty.back());
                            empty.pop_back();

                            //process
                            reverse(todo.begin(), todo.end());
                            empty.push_back(pos[todo.back()][0]);
                            for (auto& x : todo) {
                                mov(pos[x][0], pos[x][1]);
                            }
                            todo.clear();
                        } else {
                            mov(pos[t][1], pos[t][0]);
                        }
                        t = v[pos[t][1]][0];
                        if (v[pos[t][0]][1] != -1) swap(pos[t][0], pos[t][1]);
                    } else {
                        todo.push_back(t); vis[t] = 1;
                        int p = t;
                        t = v[pos[t][1]][1];
                        if (t != -1 && v[pos[t][0]][0] != p) swap(pos[t][0], pos[t][1]);
                    }
                }
            }
            swap(pos[t][0], pos[t][1]);
        }
    };

    //cout << "case2" << endl;
    //case 2 - strip
    for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        int t = i;
        clear(t);
        if (bad) return;
    }

    //cout << "case3" << endl;
    //case 3 - circle
    for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        int t = i;
        if (v[pos[t][0]][1] == t && v[pos[t][1]][1] == t) {
            vis[t] = 1;
            if (!empty.size()) { cout << -1 << endl;return; }
            mov(pos[t][0], empty.back());
            mov(pos[t][1], empty.back());
            empty.pop_back();
            t = v[pos[t][0]][0];
            clear(t); if (bad) return;
        }
    }
    // cout << "case4" << endl;
     //case 4 - finish them
    for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        int t = i; vis[t] = 1;
        if (v[pos[t][0]][1] != t) swap(pos[t][0], pos[t][1]);
        if (!empty.size()) { cout << -1 << endl;return; }
        int bk = empty.back();
        mov(pos[t][0], bk); pos[t][0] = bk;
        t = v[pos[t][0]][0];

        clear(t);
        if (bad) return;
    }



    cout << res.size() << endl;
    for (auto& x : res) cout << x.first + 1 << " " << x.second + 1 << endl;

    //cout << "result: " << endl;
    //for (auto& x : v) cout << x[0] + 1 << " " << x[1] + 1 << endl;


}

int32_t main() {
    int t = 1;
    //cin >> t;
    while (t--)
        solve();
}
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 296 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 20500 KB Output is correct
2 Correct 262 ms 22052 KB Output is correct
3 Correct 153 ms 17792 KB Output is correct
4 Correct 135 ms 17492 KB Output is correct
5 Correct 223 ms 22316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 476 KB Output is correct
10 Correct 487 ms 30440 KB Output is correct
11 Correct 114 ms 23056 KB Output is correct
12 Correct 147 ms 23100 KB Output is correct
13 Correct 440 ms 28948 KB Output is correct
14 Correct 165 ms 23732 KB Output is correct
15 Correct 148 ms 22824 KB Output is correct
16 Correct 491 ms 30780 KB Output is correct
17 Correct 138 ms 22684 KB Output is correct
18 Correct 498 ms 30148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 428 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 432 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 3 ms 428 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 1 ms 304 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 296 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 183 ms 20500 KB Output is correct
12 Correct 262 ms 22052 KB Output is correct
13 Correct 153 ms 17792 KB Output is correct
14 Correct 135 ms 17492 KB Output is correct
15 Correct 223 ms 22316 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 476 KB Output is correct
25 Correct 487 ms 30440 KB Output is correct
26 Correct 114 ms 23056 KB Output is correct
27 Correct 147 ms 23100 KB Output is correct
28 Correct 440 ms 28948 KB Output is correct
29 Correct 165 ms 23732 KB Output is correct
30 Correct 148 ms 22824 KB Output is correct
31 Correct 491 ms 30780 KB Output is correct
32 Correct 138 ms 22684 KB Output is correct
33 Correct 498 ms 30148 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 2 ms 428 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 2 ms 468 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 1 ms 308 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 2 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 3 ms 432 KB Output is correct
52 Correct 2 ms 468 KB Output is correct
53 Correct 3 ms 428 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 2 ms 468 KB Output is correct
56 Correct 1 ms 304 KB Output is correct
57 Correct 2 ms 468 KB Output is correct
58 Correct 420 ms 30808 KB Output is correct
59 Correct 467 ms 32580 KB Output is correct
60 Correct 389 ms 28772 KB Output is correct
61 Correct 437 ms 30664 KB Output is correct
62 Correct 128 ms 26140 KB Output is correct
63 Correct 401 ms 30812 KB Output is correct
64 Correct 155 ms 26052 KB Output is correct
65 Correct 521 ms 31844 KB Output is correct
66 Correct 470 ms 33252 KB Output is correct
67 Correct 149 ms 24876 KB Output is correct
68 Correct 513 ms 31784 KB Output is correct
69 Correct 466 ms 30864 KB Output is correct
70 Correct 141 ms 25124 KB Output is correct
71 Correct 152 ms 25932 KB Output is correct
72 Correct 165 ms 25700 KB Output is correct
73 Correct 489 ms 33292 KB Output is correct
74 Correct 151 ms 25280 KB Output is correct
75 Correct 474 ms 32384 KB Output is correct
76 Correct 484 ms 33052 KB Output is correct
77 Correct 516 ms 31948 KB Output is correct
78 Correct 133 ms 25692 KB Output is correct
79 Correct 461 ms 31880 KB Output is correct
80 Correct 141 ms 25204 KB Output is correct
81 Correct 541 ms 32056 KB Output is correct