結果

問題 No.5022 XOR Printer
ユーザー kaede2020
提出日時 2025-07-26 13:44:18
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,952 ms / 2,000 ms
コード長 3,662 bytes
コンパイル時間 3,200 ms
コンパイル使用メモリ 292,240 KB
実行使用メモリ 7,716 KB
スコア 3,268,590,845
最終ジャッジ日時 2025-07-26 13:46:03
合計ジャッジ時間 104,960 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int N, T;
vector<vector<int>> A0;
const vector<char> cmds = {'U','D','L','R','W','C'};

// 盤面外への移動判定
bool can_move(int x, int y, char c){
    if(c == 'U') return x > 0;
    if(c == 'D') return x < N-1;
    if(c == 'L') return y > 0;
    if(c == 'R') return y < N-1;
    return true; // W, C は常に有効
}

// 操作適用+インクリメンタルスコア更新
void apply(char c,
           vector<vector<int>>& A,
           int& x, int& y, int& s,
           ll& score){
    if(c == 'W'){
        int oldv = A[x][y];
        int newv = oldv ^ s;
        A[x][y] = newv;
        score += (newv - oldv);
    }
    else if(c == 'C'){
        s ^= A[x][y];
    }
    else {
        if(c=='U') x--;
        if(c=='D') x++;
        if(c=='L') y--;
        if(c=='R') y++;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // 入力
    cin >> N >> T;
    A0.assign(N, vector<int>(N));
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++)
            cin >> A0[i][j];

    // 事前計算:盤面の最大セル値
    int maxv = 0;
    ll initial_score = 0;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            initial_score += A0[i][j];
            maxv = max(maxv, A0[i][j]);
        }
    }

    // best_score を初期スコアで初期化
    ll best_score = initial_score;
    vector<char> best_ops;  // 空なら「何もしない」

    // ② 蛇行パターン初期解の登録
    {
        vector<vector<int>> A = A0;
        int x = 0, y = 0, s = 0;
        ll score = initial_score;

        vector<char> snake_ops;
        for(int i = 0; i < N; i++){
            if(i % 2 == 0){
                for(int j = 1; j < N; j++) snake_ops.push_back('R');
            } else {
                for(int j = 1; j < N; j++) snake_ops.push_back('L');
            }
            if(i + 1 < N) snake_ops.push_back('D');
        }

        for(char c : snake_ops){
            apply(c, A, x, y, s, score);
        }
        if(score > best_score){
            best_score = score;
            best_ops = snake_ops;
        }
    }

    // 乱数初期化
    mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> dist(0, (int)cmds.size() - 1);

    // 1.95秒タイムリミット
    auto start = chrono::steady_clock::now();
    auto limit = start + chrono::milliseconds(1950);

    // 乱数シミュレーション+早期打ち切り
    while(chrono::steady_clock::now() < limit){
        vector<vector<int>> A = A0;
        int x = 0, y = 0, s = 0;
        ll current_score = initial_score;
        vector<char> ops;
        ops.reserve(T);

        for(int t = 0; t < T; t++){
            // ランダム選択
            char c;
            do {
                c = cmds[dist(mt)];
            } while(!can_move(x, y, c));

            apply(c, A, x, y, s, current_score);
            ops.push_back(c);

            // ⑧ 早期打ち切り: 残りステップで追いつけなければ終了
            ll upper_bound = current_score + ll(T - t - 1) * maxv;
            if(upper_bound < best_score) 
                break;

            // 中間ベスト更新
            if(current_score > best_score){
                best_score = current_score;
                best_ops = ops;  // この時点までのプレフィックスを記録
            }
        }
    }

    // 出力(M ≤ T の best_ops)
    for(char c : best_ops){
        cout << c << "\n";
    }
    cerr << "Best score: " << best_score << "\n";
    return 0;
}
0