답안 #236638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
236638 2020-06-02T16:19:44 Z Sorting CEOI16_icc (CEOI16_icc) C++14
100 / 100
150 ms 760 KB
#include <bits/stdc++.h>
#include "icc.h"

using namespace std;

int query(int size_a, int size_b, int a[], int b[]);
void setRoad(int a, int b);

const int k_N = 100 + 3;

vector<vector<int>> components;
int log_table[k_N];

int query(const vector<int> &a_v, const vector<int> &b_v){
    static int a[k_N], b[k_N];

    for(int i = 0; i < a_v.size(); ++i)
        a[i] = a_v[i];
    for(int i = 0; i < b_v.size(); ++i)
        b[i] = b_v[i];

    return query(a_v.size(), b_v.size(), a, b);
}

void unite(int index_1, int index_2){
    if(components[index_1].size() < components[index_2].size())
        swap(index_1, index_2);

    for(int node: components[index_2])
        components[index_1].push_back(node);

    swap(components[index_2], components.back());
    components.pop_back();
}

void run(int n){
    components.clear();
    components.resize(n);

    for(int i = 1; i <= n; ++i)
        components[i - 1].push_back(i);

    memset(log_table, 0, sizeof(log_table));
    for(int i = 2; i < k_N; i *= 2)
        log_table[i]++;
    for(int i = 1; i < k_N; ++i)
        log_table[i] += log_table[i - 1];

    for(int i = 0; i < n - 1; ++i){
        int log_c = log_table[components.size() - 1];
        int diff = 0;

        for(int bit = 0; bit <= log_c; ++bit){
            vector<int> a, b;

            for(int j = 0; j < components.size(); ++j){
                if(j & (1 << bit)){
                    for(int x: components[j])
                        a.push_back(x);
                }
                else{
                    for(int x: components[j])
                        b.push_back(x);
                }
            }

            if(query(a, b))
                diff += (1 << bit);
        }

        vector<pair<int, int>> pairs;
        for(int i = 0; i < components.size(); ++i)
            if((i ^ diff) > i && (i ^ diff) < components.size())
                pairs.push_back({i, (i ^ diff)});

        int l = 0, r = pairs.size() - 1;
        while(l != r){
            int mid = (l + r) >> 1;

            vector<int> a, b;
            for(int i = l; i <= mid; ++i){
                for(int x: components[pairs[i].first])
                    a.push_back(x);
                for(int x: components[pairs[i].second])
                    b.push_back(x);
            }

            if(query(a, b))
                r = mid;
            else
                l = mid + 1;
        }

        int c1 = pairs[l].first, c2 = pairs[l].second;

        l = 0, r = components[c1].size() - 1;
        while(l != r){
            int mid = (l + r) >> 1;

            vector<int> a, b;
            for(int i = l; i <= mid; ++i)
                a.push_back(components[c1][i]);
            for(int x: components[c2])
                b.push_back(x);

            if(query(a, b))
                r = mid;
            else
                l = mid + 1;
        }

        int u = components[c1][l];

        l = 0, r = components[c2].size() - 1;
        while(l != r){
            int mid = (l + r) >> 1;

            vector<int> a, b;
            for(int i = l; i <= mid; ++i)
                a.push_back(components[c2][i]);
            for(int x: components[c1])
                b.push_back(x);

            if(query(a, b))
                r = mid;
            else
                l = mid + 1;
        }
        
        int v = components[c2][l];

        setRoad(u, v);

        unite(c1, c2);
    } 
}

Compilation message

icc.cpp: In function 'int query(const std::vector<int>&, const std::vector<int>&)':
icc.cpp:17:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < a_v.size(); ++i)
                    ~~^~~~~~~~~~~~
icc.cpp:19:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < b_v.size(); ++i)
                    ~~^~~~~~~~~~~~
icc.cpp: In function 'void run(int)':
icc.cpp:56:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j = 0; j < components.size(); ++j){
                            ~~^~~~~~~~~~~~~~~~~~~
icc.cpp:72:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < components.size(); ++i)
                        ~~^~~~~~~~~~~~~~~~~~~
icc.cpp:73:45: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if((i ^ diff) > i && (i ^ diff) < components.size())
                                  ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 512 KB Ok! 109 queries used.
2 Correct 12 ms 640 KB Ok! 105 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 632 KB Ok! 603 queries used.
2 Correct 43 ms 640 KB Ok! 460 queries used.
3 Correct 43 ms 632 KB Ok! 512 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 149 ms 512 KB Ok! 1506 queries used.
2 Correct 117 ms 512 KB Ok! 1077 queries used.
3 Correct 145 ms 632 KB Ok! 1498 queries used.
4 Correct 144 ms 632 KB Ok! 1454 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 150 ms 512 KB Ok! 1463 queries used.
2 Correct 148 ms 512 KB Ok! 1478 queries used.
3 Correct 148 ms 512 KB Ok! 1481 queries used.
4 Correct 144 ms 512 KB Ok! 1487 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 636 KB Ok! 1479 queries used.
2 Correct 143 ms 560 KB Ok! 1450 queries used.
3 Correct 140 ms 760 KB Ok! 1372 queries used.
4 Correct 144 ms 512 KB Ok! 1492 queries used.
5 Correct 149 ms 512 KB Ok! 1515 queries used.
6 Correct 150 ms 512 KB Ok! 1517 queries used.
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 512 KB Ok! 1427 queries used.
2 Correct 127 ms 512 KB Ok! 1210 queries used.