답안 #799840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799840 2023-08-01T06:06:05 Z 이동현(#10084) 카멜레온의 사랑 (JOI20_chameleon) C++17
100 / 100
27 ms 492 KB
#include "chameleon.h"
#include <bits/stdc++.h>
using namespace std;

namespace {

int variable_example = 1;

}  // namespace

vector<int> way[1004];
int chk[1004];

vector<int> l, r;

void Solve(int n) {
    for(int i = 1; i <= n * 2; ++i){
        auto add = [&](vector<int> x, int now){
            auto ask = x;
            ask.push_back(now);
            if(Query(ask) == (int)ask.size()){
                return;
            }

            int pos = 0;
            while(pos < (int)x.size() && (int)way[i].size() < 3){
                int low = pos, high = (int)x.size();
                while(low < high){
                    int mid = low + high >> 1;

                    vector<int> inask;
                    for(int j = low; j <= mid; ++j){
                        inask.push_back(x[j]);
                    }
                    inask.push_back(now);

                    if(Query(inask) == (int)inask.size()){
                        low = mid + 1;
                    }
                    else{
                        high = mid;
                    }
                }

                if(low == (int)x.size()) return;

                way[now].push_back(x[low]);
                way[x[low]].push_back(now);
                pos = low + 1;
            }
        };

        add(l, i);
        int sz1 = (int)way[i].size();
        add(r, i);
        int sz2 = (int)way[i].size();


        vector<int> vis(n * 2 + 4);
        auto flip = [&](auto&&self, int x, int col)->void{
            if(vis[x]) return;
            vis[x] = 1;
            if(col == 1){
                int p = 0;
                while(x != r[p]) ++p;
                swap(r[p], r.back());
                r.pop_back();
                l.push_back(x);
            }
            else{
                int p = 0;
                while(x != l[p]) ++p;
                swap(l[p], l.back());
                l.pop_back();
                r.push_back(x);
            }

            for(auto&nxt:way[x]){
                if(nxt > i) continue;
                self(self, nxt, 1 - col);
            }
        };

        if(sz1){
            r.push_back(i);
            vis[i] = 1;
            for(int j = sz1; j < sz2; ++j){
                flip(flip, way[i][j], 1);
            }
        }
        else{
            l.push_back(i);
        }
    }

    // if((int)l.size() != n || (int)r.size() != n) while(true){}

    auto del = [&](int x, int y)->void{
        for(int i = 0; i < (int)way[x].size(); ++i){
            if(way[x][i] == y){
                swap(way[x][i], way[x].back());
                way[x].pop_back();
                return;
            }
        }
        assert(0);
    };

    while(true){
        int ed = 1;
        for(int i = 1; i <= n * 2; ++i){
            if((int)way[i].size() < 3){
                continue;
            }

            ed = 0;
            assert((int)way[i].size() == 3);
            for(int j = 0; j < 3; ++j){
                vector<int> ask = {i};
                for(int x = 0; x < 3; ++x){
                    if(j != x){
                        ask.push_back(way[i][x]);
                    }
                }

                int rv = (j < 2 ? Query(ask) : 1);
                if(rv == 1){
                    // del(way[i][j], i);
                    del(i, way[i][j]);

                    break;
                }
            }

            assert((int)way[i].size() == 2);
        }

        if(ed) break;
    }

    for(int i = 1; i <= n * 2; ++i){
        for(int j = 0; j < (int)way[i].size(); ++j){
            int nxt = way[i][j];
            int fd = 0;
            for(auto&nnxt:way[nxt]){
                fd |= nnxt == i;
            }
            if(!fd){
                swap(way[i][j], way[i].back());
                way[i].pop_back();
            }
        }
    }

    vector<vector<int>> ans;
    while((int)ans.size() < n){
        for(int i = 1; i <= n * 2; ++i){
            if(chk[i] || (int)way[i].size() > 1) continue;

            assert((int)way[i].size() == 1);

            int nxt = way[i][0];
            chk[i] = chk[nxt] = 1;
            ans.push_back({i, nxt});

            for(auto&j:way[nxt]){
                del(j, nxt);
            }
            way[nxt].clear();
            way[i].clear();
        }
    }

    for(auto&i:ans){
        Answer(i[0], i[1]);
    }
}

Compilation message

chameleon.cpp: In lambda function:
chameleon.cpp:29:35: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |                     int mid = low + high >> 1;
      |                               ~~~~^~~~~~
chameleon.cpp: At global scope:
chameleon.cpp:7:5: warning: '{anonymous}::variable_example' defined but not used [-Wunused-variable]
    7 | int variable_example = 1;
      |     ^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 13 ms 464 KB Output is correct
4 Correct 13 ms 464 KB Output is correct
5 Correct 13 ms 424 KB Output is correct
6 Correct 15 ms 440 KB Output is correct
7 Correct 13 ms 448 KB Output is correct
8 Correct 13 ms 464 KB Output is correct
9 Correct 16 ms 424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 336 KB Output is correct
5 Correct 0 ms 336 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 0 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 336 KB Output is correct
5 Correct 0 ms 336 KB Output is correct
6 Correct 0 ms 336 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 0 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 19 ms 444 KB Output is correct
4 Correct 19 ms 432 KB Output is correct
5 Correct 19 ms 440 KB Output is correct
6 Correct 19 ms 428 KB Output is correct
7 Correct 19 ms 440 KB Output is correct
8 Correct 19 ms 448 KB Output is correct
9 Correct 19 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 13 ms 464 KB Output is correct
4 Correct 13 ms 464 KB Output is correct
5 Correct 13 ms 424 KB Output is correct
6 Correct 15 ms 440 KB Output is correct
7 Correct 13 ms 448 KB Output is correct
8 Correct 13 ms 464 KB Output is correct
9 Correct 16 ms 424 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 0 ms 336 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 0 ms 336 KB Output is correct
14 Correct 0 ms 336 KB Output is correct
15 Correct 0 ms 336 KB Output is correct
16 Correct 0 ms 336 KB Output is correct
17 Correct 0 ms 336 KB Output is correct
18 Correct 0 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 0 ms 336 KB Output is correct
29 Correct 0 ms 336 KB Output is correct
30 Correct 19 ms 444 KB Output is correct
31 Correct 19 ms 432 KB Output is correct
32 Correct 19 ms 440 KB Output is correct
33 Correct 19 ms 428 KB Output is correct
34 Correct 19 ms 440 KB Output is correct
35 Correct 19 ms 448 KB Output is correct
36 Correct 19 ms 468 KB Output is correct
37 Correct 26 ms 452 KB Output is correct
38 Correct 13 ms 448 KB Output is correct
39 Correct 27 ms 480 KB Output is correct
40 Correct 27 ms 468 KB Output is correct
41 Correct 27 ms 464 KB Output is correct
42 Correct 13 ms 440 KB Output is correct
43 Correct 19 ms 432 KB Output is correct
44 Correct 27 ms 464 KB Output is correct
45 Correct 27 ms 492 KB Output is correct