結果

問題 No.2977 Kth Xor Pair
ユーザー Yagnik sakhiya
提出日時 2024-12-07 23:38:09
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,952 bytes
コンパイル時間 866 ms
コンパイル使用メモリ 80,692 KB
実行使用メモリ 91,264 KB
最終ジャッジ日時 2024-12-07 23:38:24
合計ジャッジ時間 14,729 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 2
other WA * 4 RE * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <queue>

using namespace std;

class TrieNode {
public:
    TrieNode* children[2];
    TrieNode() {
        children[0] = nullptr;
        children[1] = nullptr;
    }
};

class Trie {
private:
    TrieNode* root;

public:
    Trie() {
        root = new TrieNode();
    }

    void insert(int num) {
        TrieNode* node = root;
        for (int i = 31; i >= 0; --i) { // Assuming 32-bit integers
            int bit = (num >> i) & 1;
            if (!node->children[bit]) {
                node->children[bit] = new TrieNode();
            }
            node = node->children[bit];
        }
    }

    int findMinXor(int num) {
        TrieNode* node = root;
        int xorValue = 0;
        for (int i = 31; i >= 0; --i) {
            int bit = (num >> i) & 1;
            // Prefer to go in the opposite direction for minimum XOR
            if (node->children[1 - bit]) {
                xorValue |= (1 << i);
                node = node->children[1 - bit];
            } else {
                node = node->children[bit];
            }
        }
        return xorValue;
    }
};

int main() {
    int N, K;
    cin >> N >> K;  // Read size of array and position K
    vector<int> A(N);
    
    // Read array elements
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }

    set<int> uniqueXors; // To store unique XOR results
    Trie trie;

    // Insert first element into trie
    trie.insert(A[0]);

    // Calculate all Ai ⊕ Aj for 1 ≤ i < j ≤ N using trie
    for (int i = 1; i < N; ++i) {
        int minXor = trie.findMinXor(A[i]);
        uniqueXors.insert(minXor);
        trie.insert(A[i]); // Insert current element into trie
    }

    // Convert set to vector and sort it
    vector<int> sortedResults(uniqueXors.begin(), uniqueXors.end());
    
    // Output the K-th value (1-based index)
    cout << sortedResults[K - 1] << endl;

    return 0;
}
0