답안 #972549

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
972549 2024-04-30T14:38:08 Z steveonalex 카멜레온의 사랑 (JOI20_chameleon) C++17
60 / 100
31 ms 600 KB
#include <bits/stdc++.h>
#include "chameleon.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

// namespace Grader{
//     int n;
//     vector<int> Y, C, L;
//     vector<int> check;
//     int o_cnt, ans;

//     int Query(const vector<int> &p){
//         o_cnt++;
//         vector<bool> state(n * 2 + 1);
//         for(int i: p) state[i] = 1;
//         vector<bool> color(n*2+1);
//         for(int i: p){
//             if (state[L[i]]) color[C[L[i]]] = 1;
//             else color[C[i]] = 1;
//         }

//         int ans = 0;
//         for(int i = 1; i<=2*n; ++i) ans += color[i];
//         return ans;
//     }

//     void Answer(int x, int y){
//         // cout << "! " << x << " " << y << "\n";
//         if (C[x] != C[y]) ans = false;
//         check[x]++; check[y]++;
//     }

    namespace Sub1{
        void Solve(int n){
            vector<vector<int>> graph(n*2+1);
            for(int i = 1; i<=2*n; ++i) for(int j = i + 1; j<=2*n; ++j){
                vector<int> ask = {i, j};
                if (Query(ask) == 1) {
                    graph[i].push_back(j);
                    graph[j].push_back(i);
                }
            }

            for(int i = 1; i<=2*n; ++i) if (graph[i].size() != 1){
                pair<int, int> legit = {-1, -1};
                for(int j = 0; j < 3; ++j) for(int k = j + 1; k < 3; ++k){
                    vector<int> ask = {i, graph[i][j], graph[i][k]};
                    if (Query(ask) == 1) {
                        legit = {graph[i][j], graph[i][k]};
                        break;
                    }
                }

                graph[i] = {legit.first, legit.second};
            }

            for(int i = 1; i<=2*n; ++i) sort(ALL(graph[i]));
            for(int i = 1; i<=2*n; ++i){
                for(int j: graph[i]) if (j > i){
                    if (binary_search(ALL(graph[j]), i)) Answer(i, j);
                }
            }
        }
    }
    namespace Sub2{

        void figure_out(int i, vector<vector<int>> &graph, const vector<int> &Y){
            int n = Y.size();
            if (graph[i].size()) return;
            vector<int> tmp = Y;
            vector<int> ask = tmp;
            ask.push_back(i);
            if (Query(ask) == n){
                int l = 0, r = tmp.size()- 1;
                while(l < r){
                    int mid = (l + r) >> 1;
                    vector<int> ask;
                    for(int i = 0; i<=mid; ++i) ask.push_back(tmp[i]);
                    ask.push_back(i);
                    if (Query(ask) == mid + 1) r = mid;
                    else l = mid + 1;
                }
                graph[i].push_back(tmp[l]);
                return;
            }
            int l = 0, r = tmp.size() - 1;
            while(l < r){
                int mid = (l + r) >> 1;
                vector<int> ask;
                for(int i= 0; i<=mid; ++i) ask.push_back(tmp[i]);
                ask.push_back(i);
                if (Query(ask) == mid) r = mid;
                else l = mid + 1;
            }

            graph[i].push_back(tmp[l]);
            int u = tmp[l];
            if (binary_search(ALL(graph[u]), i)) return;
            tmp.erase(tmp.begin() + l);

            l = 0, r = tmp.size() - 1;
            while(l < r){
                int mid = (l + r) >> 1;
                vector<int> ask;
                for(int i= 0; i<=mid; ++i) ask.push_back(tmp[i]);
                ask.push_back(u); ask.push_back(i);
                if (Query(ask) == mid + 1) r = mid;
                else l = mid + 1;
            }

            graph[i].push_back(tmp[l]);

            sort(ALL(graph[i]));
        }

        void Solve(int n){
            vector<int> X, Y; 
            for(int i = 1; i<=n; ++i) {
                X.push_back(i);
                Y.push_back(i+n);
            }

            vector<vector<int>> graph(n*2+1);

            shuffle(ALL(X), rng);
            shuffle(ALL(Y), rng);

            for(int i: X){
                vector<int> tmp = Y;
                vector<int> ask = tmp;
                ask.push_back(i);
                if (Query(ask) == n){
                    int l = 0, r = Y.size()- 1;
                    while(l < r){
                        int mid = (l + r) >> 1;
                        vector<int> ask;
                        for(int i = 0; i<=mid; ++i) ask.push_back(Y[i]);
                        ask.push_back(i);
                        if (Query(ask) == mid + 1) r = mid;
                        else l = mid + 1;
                    }

                    graph[i].push_back(Y[l]);
                    graph[Y[l]].push_back(i);
                }
                else{
                    vector<int> tmp = Y;
                    int l = 0, r = tmp.size() - 1;
                    while(l < r){
                        int mid = (l + r) >> 1;
                        vector<int> ask;
                        for(int i= 0; i<=mid; ++i) ask.push_back(tmp[i]);
                        ask.push_back(i);
                        if (Query(ask) == mid) r = mid;
                        else l = mid + 1;
                    }

                    graph[i].push_back(tmp[l]);
                    int u = tmp[l];
                    figure_out(u, graph, X);
                    if (binary_search(ALL(graph[u]), i)) continue;
                    tmp.erase(tmp.begin() + l);

                    l = 0, r = tmp.size() - 1;
                    while(l < r){
                        int mid = (l + r) >> 1;
                        vector<int> ask;
                        for(int i= 0; i<=mid; ++i) ask.push_back(tmp[i]);
                        ask.push_back(u); ask.push_back(i);
                        if (Query(ask) == mid + 1) r = mid;
                        else l = mid + 1;
                    }

                    graph[i].push_back(tmp[l]);
                    graph[tmp[l]].push_back(i);
                }
                sort(ALL(graph[i]));
            }
            swap(X, Y);


            for(int i = 1; i<=2*n; ++i){
                for(int j: graph[i]) if (j > i){
                    if (binary_search(ALL(graph[j]), i)) Answer(i, j);
                }
            }

        }


    }

    void Solve(int n){
        if (n <= 50) Sub1::Solve(n);
        else Sub2::Solve(n);
    }

//     pair<bool, int> run_test(int _n){
//         n = _n;
//         Y.resize(n * 2 + 1); L = C = Y;
//         for(int i= 1; i<=2*n; ++i) cin >> Y[i];
//         for(int i= 1; i<=2*n; ++i) cin >> C[i];
//         for(int i= 1; i<=2*n; ++i) cin >> L[i];
//         o_cnt = 0; ans = 1;
//         check = vector<int>(2*n+1);

//         Solve(n);

//         if (*max_element(1+ALL(check)) != 1 || *min_element(1+ALL(check)) != 1) ans = false;
//         return {ans, o_cnt};
//     }
// }

// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//     freopen("input.inp", "r", stdin);
//     // freopen("input.inp", "w", stdout);

//     // int n; cin >> n;
//     // vector<int> Y(n*2+1), C(n*2+1), L(n*2+1);
//     // for(int i= 1; i<=2*n; ++i) cin >> Y[i];
//     // for(int i= 1; i<=2*n; ++i) cin >> C[i];
//     // for(int i= 1; i<=2*n; ++i) cin >> L[i];

//     // int m = 166;

//     // cout << n*m << "\n";
//     // for(int i = 1; i<=2*n*m; ++i) cout << (i > n * m) << " "; cout << "\n";
//     // for(int j = 0; j < m; ++j) for(int i= 1; i<=3; ++i)
//     //     cout << j * n + C[i] << " "; cout << "\n";
//     // for(int j = 0; j < m; ++j) for(int i= 4; i<=6; ++i)
//     //     cout << j * n + C[i] << " "; cout << "\n";
//     // for(int j = 0; j < m; ++j) for(int i= 1; i<=3; ++i)
//     //     cout << j * n + n * (m-1) + L[i] << " "; cout << "\n";
//     // for(int j = 0; j < m; ++j) for(int i= 4; i<=6; ++i)
//     //     cout << j * n + L[i] << " "; cout << "\n";




//     int n; cin >> n;
//     pair<bool, int> verdict = Grader::run_test(n);
//     if (verdict.first) cout << "AC\n";
//     else cout << "WA\n";

//     cout << "Total operation count: " << verdict.second << "\n";

//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 26 ms 488 KB Wrong Answer [6]
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 30 ms 528 KB Output is correct
4 Correct 30 ms 352 KB Output is correct
5 Correct 30 ms 340 KB Output is correct
6 Correct 29 ms 344 KB Output is correct
7 Correct 31 ms 600 KB Output is correct
8 Correct 30 ms 344 KB Output is correct
9 Correct 29 ms 536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Incorrect 26 ms 488 KB Wrong Answer [6]
4 Halted 0 ms 0 KB -