結果

問題 No.5022 XOR Printer
ユーザー K2
提出日時 2025-07-26 15:27:02
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,988 ms / 2,000 ms
コード長 4,475 bytes
コンパイル時間 3,479 ms
コンパイル使用メモリ 293,848 KB
実行使用メモリ 7,716 KB
スコア 4,857,576,665
最終ジャッジ日時 2025-07-26 15:28:50
合計ジャッジ時間 106,719 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Timer {
    using Clock = chrono::high_resolution_clock;
    chrono::time_point<Clock> st{Clock::now()};

    double elapsed() const {
        return chrono::duration<double>(Clock::now() - st).count();
    }
};

constexpr int N = 10;
constexpr int T = 1000;
constexpr int choose = 6;

constexpr int seed = 123456789;

constexpr double TL = 1.98;

int main() {
    int n_in, t_in;
    if (!(cin >> n_in >> t_in)) return 0;

    vector<vector<int>> A(N, vector<int>(N, 0));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            cin >> A[i][j];
        }
    }
    
    Timer timer;
    mt19937 rng(seed);
    uniform_int_distribution<int> dist(0, N*N - 1);

    int best_score = -1;
    int iter = 0;
    vector<int> best_combination, best_candidate_vals;
    while (timer.elapsed() < TL) {
        vector<int> combination, candidate_vals;
        while (combination.size() < choose) {
            int idx = dist(rng);
            if (find(combination.begin(), combination.end(), idx) == combination.end()) {
                combination.emplace_back(idx);
            }
        }
        candidate_vals.emplace_back(0);
        for (int i = 0; i < choose; ++i) {
            candidate_vals.emplace_back(A[combination[i] / N][combination[i] % N] ^ candidate_vals.back());
        }
        int score = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                int val = A[i][j];
                if (find(combination.begin(), combination.end(), i * N + j) == combination.end()) {
                    for (int v : candidate_vals) {
                        val = max(val, A[i][j] ^ v);
                    }
                }
                score += val;
            }
        }
        if (score > best_score) {
            best_score = score;
            best_combination = combination;
            best_candidate_vals = candidate_vals;
        }
        iter++;
    }

    for (int idx : best_combination) {
        cerr << idx << ' ' << A[idx / N][idx % N] << endl;
    }
    cerr << "Best score: " << best_score << endl;
    cerr << "Iterations: " << iter << endl;
    
    vector<vector<int>> result(N, vector(N, -1));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            if (find(best_combination.begin(), best_combination.end(), i * N + j) != best_combination.end()) {
                continue;
            }
            int val = -1;
            int idx = -1;
            for (int k = 0; k < choose + 1; ++k) {
                int candidate_val = A[i][j] ^ best_candidate_vals[k];
                if (candidate_val > val) {
                    val = candidate_val;
                    idx = k;
                }
            }
            result[i][j] = idx;
        }
    }

    int score = 0;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            if (result[i][j] == -1) {
                score += A[i][j];
            } else {
                score += A[i][j] ^ best_candidate_vals[result[i][j]];
            }
        }
    }
    cerr << "Final score: " << score << endl;

    vector<char> output;
    output.reserve(1000);
    for (int i = 0; i < choose + 1; ++i) {
        for (int j = 0; j < N; ++j) {
            for (int k = 0; k < N; ++k) {
                int y = (j % 2 == 0) ? k : N - 1 - k;  // jが偶数ならk, 奇数ならN-1-k
                if (result[j][y] == i) {
                    output.emplace_back('W');
                }
                if (k != N-1) {
                    output.emplace_back(j % 2 ? 'L' : 'R');
                }
            }
            if (j != N-1) {
                output.emplace_back('D');
            }
        }
        if (i == choose) break;
        
        // 目標位置への移動
        int target_x = best_combination[i] / N;
        int target_y = best_combination[i] % N;
        
        // X方向の移動
        for (int j = N-1; j > target_x; --j) {
            output.emplace_back('U');
        }
        // Y方向の移動
        for (int j = 0; j < target_y; ++j) {
            output.emplace_back('R');
        }
        output.emplace_back('C');
        for (int j = target_y; j > 0; --j) {
            output.emplace_back('L');
        }
        for (int j = target_x; j > 0; --j) {
            output.emplace_back('U');
        }
    }
    for (char op : output) {
        cout << op << endl;
    }
}
0