답안 #817275

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
817275 2023-08-09T11:13:32 Z t6twotwo Parking (CEOI22_parking) C++17
100 / 100
83 ms 11136 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FFOR(i,a,b,c) for(int i=(a);(c)>0?i<=(b):i>=(b);i+=(c))
#define FOOR(i,a,b) FFOR(i,a,(b)-1,1)
#define ROOF(i,a,b) FFOR(i,(b)-1,a,-1)
#define FOR(i,n) FOOR(i,0,n)
#define ROF(i,n) ROOF(i,0,n)
#define FORR(x,v) for(auto &x:v)
#define vc vector
#define sz(v) (int)((v).size())
#define bg begin()
#define en end()
#define em empty()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define ppb pop_back()
#define eb emplace_back
#define bk back()
#define fr front()
#define pp pop()
#define all(v) (v).begin(),(v).end()
#define lla(v) (v).rbegin(),(v).rend()
#define f first
#define s second
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,M;
    cin>>N>>M;
    vc<int> bot(M),top(M),x(N,-1),y(N),free;
    FOR(i,M){
        cin>>bot[i]>>top[i];
        bot[i]--,top[i]--;
        if(bot[i]!=-1) (x[bot[i]]==-1?x[bot[i]]:y[bot[i]])=i*2;
        if(top[i]!=-1) (x[top[i]]==-1?x[top[i]]:y[top[i]])=i*2+1;
        if(bot[i]==-1&&top[i]==-1) free.pb(i);
    }
    vc<pair<int,int>> ans;
    auto move=[&](int u,int v) {
        ans.eb(u,v);
        int a;
        if(top[u]!=-1){
            a=top[u];
            top[u]=-1;
        }else{
            a=bot[u];
            bot[u]=-1;
        }
        if(bot[v]==-1){
            (x[a]/2==u?x[a]:y[a])=v*2;
            bot[v]=a;
        }else{
            (x[a]/2==u?x[a]:y[a])=v*2+1;
            top[v]=a;
        }
        if(bot[u]==-1){
            free.pb(u);
        }
    };
    auto park=[&](int i){
        int a=x[i]/2;
        int b=y[i]/2;
        if(a==b)return;
        if(top[a]!=-1&&top[b]!=-1) return;
        if(x[i]%2==1&&y[i]%2==1) return;
        if(x[i]%2==0&&top[a]!=-1) return;
        if(y[i]%2==0&&top[b]!=-1) return;
        if(top[b]!=-1) swap(a,b);
        move(a,b);
    };
    auto get=[&](){
        if(free.em){
            cout<<-1;
            exit(0);
        }
        int t=free.bk;
        free.ppb;
        return t;
    };
    FOR(i,M){
        if(top[i]==-1&&bot[i]!=-1){
            int j=i,k=bot[i];
            while(x[k]%2!=y[k]%2){
                j^=(x[k]/2)^(y[k]/2);
                park(k);
                k=bot[j];
            }
        }
    }
    FOR(i,N) park(i);
    vc<bool> vis(M);
    FOR(i,M){
        if(vis[i]||bot[i]==top[i]||top[i]!=-1)continue;
        vis[i]=1;
        int k=bot[i],f=0,j=i^(x[k]/2)^(y[k]/2);
        vc<int> S{k};
        vc<vc<int>> T;
        while(1){
            T.pb({});
            vis[j]=1;
            k=f?bot[j]:top[j];
            while(k!=-1&&x[k]%2!=y[k]%2){
                T.bk.pb(k);
                j^=(x[k]/2)^(y[k]/2);
                vis[j]=1;
                k=f?bot[j]:top[j];
            }
            if (k==-1)break;
            S.pb(k);
            j^=(x[k]/2)^(y[k]/2);
            f^=1;
        }
        FFOR(j,1,sz(S)-2,2){
            int t=get();
            move(x[S[j]]/2,t);
            move(y[S[j]]/2,t);
            ROF(k,sz(T[j-1])) park(T[j-1][k]);
            FOR(k,sz(T[j])) park(T[j][k]);
            park(S[j-1]);
        }
        park(S.bk);
    }
    FOR(i,M){
        if(vis[i]||bot[i]==top[i])continue;
        vis[i]=1;
        int k=bot[i],f=0,j=i;
        vc<int> S{k};
        vc<vc<int>> T;
        while(1){
            T.pb({});
            vis[j]=1;
            k=f?bot[j]:top[j];
            while(k!=bot[i]&&x[k]%2!=y[k]%2){
                T.bk.pb(k);
                j^=(x[k]/2)^(y[k]/2);
                vis[j]=1;
                k=f?bot[j]:top[j];
            }
            if (k==bot[i])break;
            S.pb(k);
            j^=(x[k]/2)^(y[k]/2);
            f^=1;
        }
        if (sz(S)==1){
            int t=get();
            int a=x[S[0]];
            int b=y[S[0]];
            if (a%2==0) swap(a,b);
            move(a/2,t);
            ROF(j,sz(T[0])) park(T[0][j]);
            move(b/2,t);
            continue;
        }
        int t=get();
        move(x[S[1]]/2,t);
        move(y[S[1]]/2,t);
        ROF(j,sz(T[0])) park(T[0][j]);
        FOR(j,sz(T[1])) park(T[1][j]);
        FFOR(j,3,sz(S)-1,2){
            int t=get();
            move(x[S[j]]/2,t);
            move(y[S[j]]/2,t);
            ROF(k,sz(T[j-1])) park(T[j-1][k]);
            FOR(k,sz(T[j])) park(T[j][k]);
            park(S[j-1]);
        }
        park(S[0]);
        if(sz(S)%2){
            ROF(j,sz(T.bk)) park(T.bk[j]);
            park(S.bk);
        }
    }
    cout<<sz(ans)<<"\n";
    for(auto[x,y]:ans){
        cout<<x+1<<" "<<y+1<<"\n";
    }
    return 6/22;
}
# 결과 실행 시간 메모리 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 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 4696 KB Output is correct
2 Correct 52 ms 5140 KB Output is correct
3 Correct 31 ms 4132 KB Output is correct
4 Correct 30 ms 4044 KB Output is correct
5 Correct 42 ms 5148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 77 ms 8548 KB Output is correct
11 Correct 28 ms 3380 KB Output is correct
12 Correct 31 ms 3372 KB Output is correct
13 Correct 67 ms 7512 KB Output is correct
14 Correct 32 ms 3436 KB Output is correct
15 Correct 31 ms 3336 KB Output is correct
16 Correct 79 ms 8680 KB Output is correct
17 Correct 31 ms 3284 KB Output is correct
18 Correct 75 ms 8220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 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 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 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 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 37 ms 4696 KB Output is correct
12 Correct 52 ms 5140 KB Output is correct
13 Correct 31 ms 4132 KB Output is correct
14 Correct 30 ms 4044 KB Output is correct
15 Correct 42 ms 5148 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 77 ms 8548 KB Output is correct
26 Correct 28 ms 3380 KB Output is correct
27 Correct 31 ms 3372 KB Output is correct
28 Correct 67 ms 7512 KB Output is correct
29 Correct 32 ms 3436 KB Output is correct
30 Correct 31 ms 3336 KB Output is correct
31 Correct 79 ms 8680 KB Output is correct
32 Correct 31 ms 3284 KB Output is correct
33 Correct 75 ms 8220 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 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 1 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 1 ms 320 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 79 ms 10072 KB Output is correct
59 Correct 79 ms 11036 KB Output is correct
60 Correct 65 ms 9196 KB Output is correct
61 Correct 73 ms 10112 KB Output is correct
62 Correct 31 ms 5964 KB Output is correct
63 Correct 63 ms 9796 KB Output is correct
64 Correct 36 ms 5860 KB Output is correct
65 Correct 76 ms 10660 KB Output is correct
66 Correct 83 ms 11104 KB Output is correct
67 Correct 31 ms 5596 KB Output is correct
68 Correct 76 ms 10300 KB Output is correct
69 Correct 64 ms 9780 KB Output is correct
70 Correct 32 ms 5736 KB Output is correct
71 Correct 32 ms 5860 KB Output is correct
72 Correct 32 ms 5836 KB Output is correct
73 Correct 79 ms 11136 KB Output is correct
74 Correct 32 ms 5760 KB Output is correct
75 Correct 81 ms 10868 KB Output is correct
76 Correct 78 ms 11068 KB Output is correct
77 Correct 75 ms 10684 KB Output is correct
78 Correct 32 ms 5836 KB Output is correct
79 Correct 76 ms 10700 KB Output is correct
80 Correct 31 ms 5708 KB Output is correct
81 Correct 72 ms 10512 KB Output is correct