結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー prussian_coder
提出日時 2026-11-10 21:37:29
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,792 ms / 2,000 ms
+ 839µs
コード長 3,867 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,957 ms
コンパイル使用メモリ 370,448 KB
実行使用メモリ 6,400 KB
スコア 1,410,409
最終ジャッジ日時 2026-11-23 20:35:40
合計ジャッジ時間 98,552 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// Step 4: 焼きなまし法(毎回スコアを全部計算し直す)
// Step 3 の山登り法を、悪くなる変更も確率 exp(差分 / 温度) で受け入れる焼きなまし法に変える。
// 温度は時間とともに START_TEMP から END_TEMP へ下げる。
#include <bits/stdc++.h>
using namespace std;

// 方向: 0=上, 1=右, 2=下, 3=左
const int DR[4] = {-1, 0, 1, 0};
const int DC[4] = {0, 1, 0, -1};
// 状態 s のマスに方向 d で入った光は、方向 d ^ REFLECT[s] へ進む
// (0: 直進, 1: '/', 2: '\')
const int REFLECT[3] = {0, 1, 3};

int N, M;
vector<int> A;

// 温度の単位はスコア(転倒数)の差
double START_TEMP = 3.0;
double END_TEMP = 0.1;

mt19937 rng(0);
uniform_real_distribution<double> uniform01(0.0, 1.0);
auto start_time = chrono::steady_clock::now();
double elapsed() {
    return chrono::duration<double>(chrono::steady_clock::now() - start_time).count();
}

// ポート p から入る光の、最初のマスと進行方向
void entrance(int p, int& r, int& c, int& d) {
    if (p < N)          { r = 0;               c = p;               d = 2; }
    else if (p < 2 * N) { r = p - N;           c = N - 1;           d = 3; }
    else if (p < 3 * N) { r = N - 1;           c = 3 * N - 1 - p;   d = 0; }
    else                { r = 4 * N - 1 - p;   c = 0;               d = 1; }
}

// 盤面の外のマス (r, c) に対応するポート番号
int exit_port(int r, int c) {
    if (r < 0) return c;
    if (c >= N) return N + r;
    if (r >= N) return 3 * N - 1 - c;
    return 4 * N - 1 - r;
}

bool inside(int r, int c) { return 0 <= r && r < N && 0 <= c && c < N; }

// ポート p から入った光が出ていくポート
int trace(const vector<vector<int>>& board, int p) {
    int r, c, d;
    entrance(p, r, c, d);
    while (inside(r, c)) {
        d ^= REFLECT[board[r][c]];
        r += DR[d];
        c += DC[d];
    }
    return exit_port(r, c);
}

// 転倒数を O(M^2) で数える
int count_inversions(const vector<int>& B) {
    int inv = 0;
    for (int i = 0; i < M; i++)
        for (int j = i + 1; j < M; j++)
            if (B[i] > B[j]) inv++;
    return inv;
}

int compute_score(const vector<vector<int>>& board) {
    vector<int> B(M);
    for (int p = 0; p < M; p++) B[trace(board, p)] = A[p];
    return M * (M - 1) / 2 - count_inversions(B);
}

int main(int argc, char** argv) {
    if (argc >= 3) { START_TEMP = atof(argv[1]); END_TEMP = atof(argv[2]); }  // 調整用
    cin >> N;
    M = 4 * N;
    A.resize(M);
    for (int& a : A) cin >> a;

    vector<vector<int>> board(N, vector<int>(N, 0));
    int score = compute_score(board);
    int best_score = score;
    vector<vector<int>> best_board = board;
    int trials = 0;

    const double TIME_LIMIT = 1.8;
    while (true) {
        double t = elapsed() / TIME_LIMIT;  // 経過時間の割合(0 → 1)
        if (t >= 1.0) break;
        double temp = START_TEMP * pow(END_TEMP / START_TEMP, t);
        trials++;
        // ランダムなマスを、今と異なるランダムな状態に変える
        int r = rng() % N, c = rng() % N;
        int old_state = board[r][c];
        board[r][c] = (old_state + 1 + rng() % 2) % 3;

        int new_score = compute_score(board);
        int delta = new_score - score;
        // 良くなるなら必ず、悪くなるなら確率 exp(delta / temp) で採用する
        if (delta >= 0 || uniform01(rng) < exp(delta / temp)) {
            score = new_score;
            if (score > best_score) {
                best_score = score;
                best_board = board;
            }
        } else {
            board[r][c] = old_state;
        }
    }

    for (auto& row : best_board) {
        for (int x : row) cout << x;
        cout << '\n';
    }
    cerr << "trials = " << trials << ", score = " << best_score << '\n';
}
0