Submission #832024

# Submission time Handle Problem Language Result Execution time Memory
832024 2023-08-20T20:30:39 Z QwertyPi Parking (CEOI22_parking) C++14
100 / 100
1846 ms 77368 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;

const int MAXN = 2e5 + 11;

map<int, vector<int>> pos;
struct slot{
    int id, a[2], cnt = 0;
    int count(){ cnt = 0; if(a[0] != 0) cnt++; if(a[1] != 0) cnt++; return cnt; }
    int top(){ if(cnt == 0) return 0; return a[cnt - 1]; }
    void remove(){ assert(cnt != 0); pos[a[cnt - 1]].erase(find(pos[a[cnt - 1]].begin(), pos[a[cnt - 1]].end(), id)); a[--cnt] = 0; }
    void add(int x, bool init = false){ assert(cnt != 2); assert(init || !(cnt == 1 && a[0] != x)); pos[x].push_back(id); a[cnt++] = x; }
    bool same(){ return a[0] == a[1] && a[0] != 0; }
    int other(int x){ assert(count() == 2); return a[0] + a[1] - x; }
} S[MAXN];

void failure(){
    cout << -1 << endl;
    exit(0);
}

int n, m; 
bool done[MAXN];

vector<pair<int, int>> ans;

map<int, vector<int>> tops, bottoms;
vector<int> immediate, single, both_top, empty_space;

void move(int x, int y){
#ifdef LOCAL
    printf("move: %d => %d\n", x, y);
#endif
    assert(1 <= x && x <= m && 1 <= y && y <= m && x != y);
    S[y].add(S[x].top()); S[x].remove();
    ans.push_back({x, y});

    if(S[x].count() == 0) bottoms[S[y].top()].erase(find(all(bottoms[S[y].top()]), x));
    tops[S[y].top()].erase(find(all(tops[S[y].top()]), x));
    tops[S[y].top()].push_back(y);

    if(S[x].count()){
        tops[S[x].top()].push_back(x);
        bottoms[S[x].top()].push_back(x);
        if(tops[S[x].top()].size() == 2){
            immediate.push_back(S[x].top());
        }
        single.push_back(S[x].top());
    }else{
        empty_space.push_back(x);
    }


    if(S[y].count() == 1){
        bottoms[S[y].top()].push_back(y);
        if(tops[S[y].top()].size() == 2){
            immediate.push_back(S[y].top());
        }
        single.push_back(S[y].top());
    }

    if(tops[S[y].top()].size() == 2){
        both_top.push_back(S[y].top());
    }
    if(S[y].same()){
        done[S[y].top()] = true;
    }
}

void print(){
    for(int i = 1; i >= 0; i--){
        for(int j = 1; j <= m; j++){
            cout << S[j].a[i] << " \n"[j == m];
        }
    }
}

int32_t main(){
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        int x, y; cin >> x >> y;
        S[i].id = i;
        if(x != 0) S[i].add(x);
        if(y != 0) S[i].add(y, true);
    }

    for(int i = 1; i <= m; i++){
        if(S[i].same()){
            done[S[i].top()] = true;
            continue;
        }
        if(S[i].count() == 0){
            empty_space.push_back(i);
        }else{
            tops[S[i].top()].push_back(i);
            if(S[i].count() == 1){
                bottoms[S[i].top()].push_back(i);
                single.push_back(S[i].top());
            }
        }
    }

    for(auto [x, v] : bottoms){
        if(tops[x].size() == 2){
            immediate.push_back(x);
        }
    }

    for(auto [x, v] : tops){
        if(v.size() == 2){
            both_top.push_back(x);
        }
    }

    int not_done = 1;
    while(not_done <= n){
        while(empty_space.size() && S[empty_space.back()].count() != 0) empty_space.pop_back();
        if(immediate.size()){
            int x = immediate.back(); immediate.pop_back();
            if(done[x]) continue;
            vector<int> top = tops[x];
            vector<int> bottom = bottoms[x];
            int from = top[0] == bottom[0] ? top[1] : top[0];
            int to = bottom[0];
            move(from, to);
            continue;
        }
        if(empty_space.size() && single.size()){
            int x = single.back(); single.pop_back();
            if(done[x]) continue;
            vector<int> st;

            vector<int> p = pos[x];
            assert(p.size() == 2);
            if(S[p[1]].count() == 1) swap(p[0], p[1]);
            assert(S[p[1]].count() == 2);
            st.push_back(S[p[1]].other(x));
            while(true){
                int y = st.back();
                vector<int> p2 = pos[y];
                assert(p2.size() == 2);
                if(S[p2[0]].top() == y && S[p2[1]].top() == y) break;
                if(S[p2[0]].top() == y) swap(p2[0], p2[1]);
                st.push_back(S[p2[0]].top());
            }
            int e = empty_space.back(); empty_space.pop_back();
            vector<int> p3 = pos[st.back()];
            move(p3[0], e);
            move(p3[1], e);
            continue;
        }
        if(empty_space.size() && both_top.size()){
            int x = both_top.back(); both_top.pop_back();
            if(done[x]) continue;
            int e = empty_space.back(); empty_space.pop_back();
            vector<int> p3 = pos[x];
            move(p3[0], e);
            move(p3[1], e);
            continue;
        }
        while(done[not_done] && not_done <= n) not_done++;
        if(empty_space.size() && not_done <= n){
            int x = not_done;
            int e = empty_space.back(); empty_space.pop_back();
            vector<int> p = pos[x];
            if(S[p[0]].top() != x) swap(p[0], p[1]);
            move(p[0], e);
            continue;
        }
        if(not_done <= n) failure();
    }
#ifdef LOCAL
    print();
#endif
    cout << ans.size() << endl;
    for(auto [x, y] : ans){
        cout << x << ' ' << y << endl;
    }
}

Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:104:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  104 |     for(auto [x, v] : bottoms){
      |              ^
Main.cpp:110:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  110 |     for(auto [x, v] : tops){
      |              ^
Main.cpp:177:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  177 |     for(auto [x, y] : ans){
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3412 KB Output is correct
2 Correct 1 ms 3412 KB Output is correct
3 Correct 1 ms 3412 KB Output is correct
4 Correct 1 ms 3412 KB Output is correct
5 Correct 1 ms 3412 KB Output is correct
6 Correct 1 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 1 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 530 ms 32356 KB Output is correct
2 Correct 645 ms 39240 KB Output is correct
3 Correct 359 ms 25236 KB Output is correct
4 Correct 312 ms 23192 KB Output is correct
5 Correct 648 ms 38452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3540 KB Output is correct
2 Correct 2 ms 3440 KB Output is correct
3 Correct 2 ms 3540 KB Output is correct
4 Correct 5 ms 3796 KB Output is correct
5 Correct 2 ms 3572 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 6 ms 3700 KB Output is correct
8 Correct 2 ms 3540 KB Output is correct
9 Correct 5 ms 3700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3540 KB Output is correct
2 Correct 2 ms 3440 KB Output is correct
3 Correct 2 ms 3540 KB Output is correct
4 Correct 5 ms 3796 KB Output is correct
5 Correct 2 ms 3572 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 6 ms 3700 KB Output is correct
8 Correct 2 ms 3540 KB Output is correct
9 Correct 5 ms 3700 KB Output is correct
10 Correct 1673 ms 76024 KB Output is correct
11 Correct 213 ms 27244 KB Output is correct
12 Correct 406 ms 48328 KB Output is correct
13 Correct 1573 ms 74352 KB Output is correct
14 Correct 440 ms 44508 KB Output is correct
15 Correct 432 ms 43028 KB Output is correct
16 Correct 1689 ms 76912 KB Output is correct
17 Correct 455 ms 45240 KB Output is correct
18 Correct 1631 ms 76180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3668 KB Output is correct
2 Correct 2 ms 3540 KB Output is correct
3 Correct 5 ms 3668 KB Output is correct
4 Correct 3 ms 3540 KB Output is correct
5 Correct 2 ms 3540 KB Output is correct
6 Correct 4 ms 3668 KB Output is correct
7 Correct 2 ms 3540 KB Output is correct
8 Correct 4 ms 3668 KB Output is correct
9 Correct 5 ms 3668 KB Output is correct
10 Correct 2 ms 3540 KB Output is correct
11 Correct 5 ms 3668 KB Output is correct
12 Correct 5 ms 3652 KB Output is correct
13 Correct 2 ms 3540 KB Output is correct
14 Correct 2 ms 3540 KB Output is correct
15 Correct 2 ms 3540 KB Output is correct
16 Correct 5 ms 3668 KB Output is correct
17 Correct 2 ms 3568 KB Output is correct
18 Correct 5 ms 3700 KB Output is correct
19 Correct 5 ms 3796 KB Output is correct
20 Correct 5 ms 3668 KB Output is correct
21 Correct 2 ms 3540 KB Output is correct
22 Correct 5 ms 3668 KB Output is correct
23 Correct 3 ms 3540 KB Output is correct
24 Correct 5 ms 3696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3412 KB Output is correct
2 Correct 1 ms 3412 KB Output is correct
3 Correct 1 ms 3412 KB Output is correct
4 Correct 1 ms 3412 KB Output is correct
5 Correct 1 ms 3412 KB Output is correct
6 Correct 1 ms 3412 KB Output is correct
7 Correct 2 ms 3412 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 1 ms 3412 KB Output is correct
11 Correct 530 ms 32356 KB Output is correct
12 Correct 645 ms 39240 KB Output is correct
13 Correct 359 ms 25236 KB Output is correct
14 Correct 312 ms 23192 KB Output is correct
15 Correct 648 ms 38452 KB Output is correct
16 Correct 4 ms 3540 KB Output is correct
17 Correct 2 ms 3440 KB Output is correct
18 Correct 2 ms 3540 KB Output is correct
19 Correct 5 ms 3796 KB Output is correct
20 Correct 2 ms 3572 KB Output is correct
21 Correct 2 ms 3540 KB Output is correct
22 Correct 6 ms 3700 KB Output is correct
23 Correct 2 ms 3540 KB Output is correct
24 Correct 5 ms 3700 KB Output is correct
25 Correct 1673 ms 76024 KB Output is correct
26 Correct 213 ms 27244 KB Output is correct
27 Correct 406 ms 48328 KB Output is correct
28 Correct 1573 ms 74352 KB Output is correct
29 Correct 440 ms 44508 KB Output is correct
30 Correct 432 ms 43028 KB Output is correct
31 Correct 1689 ms 76912 KB Output is correct
32 Correct 455 ms 45240 KB Output is correct
33 Correct 1631 ms 76180 KB Output is correct
34 Correct 4 ms 3668 KB Output is correct
35 Correct 2 ms 3540 KB Output is correct
36 Correct 5 ms 3668 KB Output is correct
37 Correct 3 ms 3540 KB Output is correct
38 Correct 2 ms 3540 KB Output is correct
39 Correct 4 ms 3668 KB Output is correct
40 Correct 2 ms 3540 KB Output is correct
41 Correct 4 ms 3668 KB Output is correct
42 Correct 5 ms 3668 KB Output is correct
43 Correct 2 ms 3540 KB Output is correct
44 Correct 5 ms 3668 KB Output is correct
45 Correct 5 ms 3652 KB Output is correct
46 Correct 2 ms 3540 KB Output is correct
47 Correct 2 ms 3540 KB Output is correct
48 Correct 2 ms 3540 KB Output is correct
49 Correct 5 ms 3668 KB Output is correct
50 Correct 2 ms 3568 KB Output is correct
51 Correct 5 ms 3700 KB Output is correct
52 Correct 5 ms 3796 KB Output is correct
53 Correct 5 ms 3668 KB Output is correct
54 Correct 2 ms 3540 KB Output is correct
55 Correct 5 ms 3668 KB Output is correct
56 Correct 3 ms 3540 KB Output is correct
57 Correct 5 ms 3696 KB Output is correct
58 Correct 1501 ms 69380 KB Output is correct
59 Correct 1659 ms 76084 KB Output is correct
60 Correct 1265 ms 61832 KB Output is correct
61 Correct 1489 ms 70608 KB Output is correct
62 Correct 223 ms 27884 KB Output is correct
63 Correct 1515 ms 74416 KB Output is correct
64 Correct 420 ms 44836 KB Output is correct
65 Correct 1552 ms 74432 KB Output is correct
66 Correct 1671 ms 77368 KB Output is correct
67 Correct 441 ms 47380 KB Output is correct
68 Correct 1562 ms 75152 KB Output is correct
69 Correct 1504 ms 73040 KB Output is correct
70 Correct 443 ms 45680 KB Output is correct
71 Correct 432 ms 44188 KB Output is correct
72 Correct 473 ms 43600 KB Output is correct
73 Correct 1846 ms 76688 KB Output is correct
74 Correct 450 ms 43208 KB Output is correct
75 Correct 1734 ms 74564 KB Output is correct
76 Correct 1781 ms 75908 KB Output is correct
77 Correct 1725 ms 73916 KB Output is correct
78 Correct 434 ms 43972 KB Output is correct
79 Correct 1692 ms 74108 KB Output is correct
80 Correct 460 ms 45576 KB Output is correct
81 Correct 1652 ms 74908 KB Output is correct