結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー twins_fuyu
提出日時 2026-11-23 22:48:43
言語 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,913 ms / 2,000 ms
+ 879µs
コード長 15,709 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,479 ms
コンパイル使用メモリ 384,452 KB
実行使用メモリ 6,400 KB
スコア 1,452,169
最終ジャッジ日時 2026-11-23 22:53:45
合計ジャッジ時間 106,371 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// ============== main.cpp ==============
#pragma GCC optimize("O3")
// library url: https://atcoder.jp/contests/intro-heuristics/submissions/80090780
// ============== define.hpp ==============
#include <bits/stdc++.h>
// ============== library/timer.hpp ==============

#include <chrono>

namespace library{

class Timer {
private:
    std::chrono::steady_clock::time_point start_time;
    double scale = 1.0;

public:
    Timer() : start_time(std::chrono::steady_clock::now()) {}

    void reset() {
        start_time = std::chrono::steady_clock::now();
    }

    void set_scale(double s) {
        scale = s;
    }

    double elapsed() const {
        auto now = std::chrono::steady_clock::now();
        std::chrono::duration<double> diff = now - start_time;
        return diff.count() * scale;
    }
};

}
// ============== End of library/timer.hpp ==============


// ============== library/rng.hpp ==============

#include <array>
#include <chrono>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <tuple>
#include <utility>

namespace library {

// ============================================================
// utilities
// ============================================================

constexpr uint64_t rotl64(uint64_t x, int k) noexcept
{
    return (x << k) | (x >> (64 - k));
}

constexpr uint64_t splitmix64_next(uint64_t& x) noexcept
{
    x += 0x9E3779B97F4A7C15ULL;
    uint64_t z = x;
    z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
    z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
    return z ^ (z >> 31);
}

inline uint64_t random_chrono()
{
    uint64_t t =
        static_cast<uint64_t>(
            std::chrono::high_resolution_clock::now().time_since_epoch().count());
    return splitmix64_next(t);
}

// ============================================================
// base class / concept
// ============================================================

struct rng_base_u64 {
    using result_type = uint64_t;

    static constexpr result_type min() noexcept
    {
        return 0;
    }

    static constexpr result_type max() noexcept
    {
        return std::numeric_limits<result_type>::max();
    }
};

// ============================================================
// xorplus
// - 超低品質、超高速乱数
// ============================================================

class xorplus : public rng_base_u64 {
private:
    uint64_t x1 = 0;
    uint64_t x2 = 0;

public:
    constexpr explicit xorplus(uint64_t seed = 1) noexcept
    {
        this->seed(seed);
    }

    constexpr void seed(uint64_t seed_val) noexcept
    {
        uint64_t x = seed_val;
        x1 = splitmix64_next(x);
        x2 = splitmix64_next(x);
        if (x1 == 0 && x2 == 0) {
            seed(seed_val + 1);
        }
    }

    constexpr result_type operator()() noexcept
    {
        x1 += x2;
        x2 ^= x1;
        return x1;
    }
};

// ============================================================
// xoroplus
// - 中低品質(?) 、高速
// - xorplusと比べれば大幅に高品質 (randint3テスト、ベルヌーイテストともに良好)
// ============================================================

class xoroplus : public rng_base_u64 {
private:
    uint64_t x1 = 0;
    uint64_t x2 = 0;

public:
    constexpr explicit xoroplus(uint64_t seed = 1) noexcept
    {
        this->seed(seed);
    }

    constexpr void seed(uint64_t seed_val) noexcept
    {
        uint64_t x = seed_val;
        x1 = splitmix64_next(x);
        x2 = splitmix64_next(x);
        if (x1 == 0 && x2 == 0) {
            seed(seed_val + 1);
        }
    }

    constexpr result_type operator()() noexcept
    {
        x1 += rotl64(x2, 17);
        x2 ^= x1;
        return x1;
    }
};

// ============================================================
// xoshiro256
// - 高速で品質もそこそこな乱数
// ============================================================

class xoshiro256 : public rng_base_u64 {
private:
    uint64_t s_[4]{};
public:
    constexpr explicit xoshiro256(uint64_t seed = 1) noexcept
    {
        this->seed(seed);
    }

    constexpr void seed(uint64_t seed_val) noexcept
    {
        uint64_t x = seed_val;
        s_[0] = splitmix64_next(x);
        s_[1] = splitmix64_next(x);
        s_[2] = splitmix64_next(x);
        s_[3] = splitmix64_next(x);

        if (s_[0] == 0 && s_[1] == 0 && s_[2] == 0 && s_[3] == 0) {
            s_[3] = 1;
        }
    }

    constexpr result_type operator()() noexcept
    {
        step();
        return s_[0];
    }

    constexpr void step() noexcept
    {
        const uint64_t t = s_[1] << 17;

        s_[2] ^= s_[0];
        s_[3] ^= s_[1];
        s_[1] ^= s_[2];
        s_[0] ^= s_[3];

        s_[2] ^= t;
        s_[3] = rotl64(s_[3], 45);
    }
    constexpr uint64_t get(int i) const noexcept {
        return s_[i];
    }
};

// ============================================================
// xoshiro256++
// - 高品質な乱数(ただしやや遅い)
// ============================================================

class xoshiro256pp : public rng_base_u64 {
private:
    uint64_t s_[4]{};

public:
    constexpr explicit xoshiro256pp(uint64_t seed = 1) noexcept
    {
        this->seed(seed);
    }

    constexpr void seed(uint64_t seed_val) noexcept
    {
        uint64_t x = seed_val;
        s_[0] = splitmix64_next(x);
        s_[1] = splitmix64_next(x);
        s_[2] = splitmix64_next(x);
        s_[3] = splitmix64_next(x);

        if (s_[0] == 0 && s_[1] == 0 && s_[2] == 0 && s_[3] == 0) {
            seed(seed_val + 1);
        }
    }

    constexpr result_type operator()() noexcept
    {
        const uint64_t result = rotl64(s_[0] + s_[3], 23) + s_[0];
        const uint64_t t = s_[1] << 17;

        s_[2] ^= s_[0];
        s_[3] ^= s_[1];
        s_[1] ^= s_[2];
        s_[0] ^= s_[3];

        s_[2] ^= t;
        s_[3] = rotl64(s_[3], 45);

        return result;
    }
};

// ============================================================
// randint helpers
// - 64bit RNG 専用
// ============================================================

inline int randint(int r, uint64_t rng_val)
{
    return static_cast<int>((static_cast<uint64_t>(rng_val >> 32) * static_cast<uint64_t>(r)) >> 32);
}

inline int randint(int l, int r, uint64_t rng_val)
{
    return l + randint(r - l, rng_val);
}

inline std::pair<int, int> randint_pair(int r, uint64_t rng_val)
{
    const uint64_t v = rng_val;
    return {
        static_cast<int>(((v >> 32) * static_cast<uint64_t>(r)) >> 32),
        static_cast<int>(((v & 0xFFFFFFFFULL) * static_cast<uint64_t>(r)) >> 32)
    };
}

inline std::pair<int, int> randint_pair(int l, int r, uint64_t rng_val)
{
    const uint64_t v = rng_val;
    const uint64_t d = static_cast<uint64_t>(r - l);
    return {
        l + static_cast<int>(((v >> 32) * d) >> 32),
        l + static_cast<int>(((v & 0xFFFFFFFFULL) * d) >> 32)
    };
}

inline std::tuple<int, int, int> randint3(int r, uint64_t rng_val)
{
    const uint64_t v1 = rng_val >> 42;
    const uint64_t v2 = (rng_val >> 21) & 0x1FFFFFULL;
    const uint64_t v3 = rng_val & 0x1FFFFFULL;
    const uint64_t rr = static_cast<uint64_t>(r);

    return {
        static_cast<int>((v1 * rr) >> 22),
        static_cast<int>((v2 * rr) >> 21),
        static_cast<int>((v3 * rr) >> 21)
    };
}

inline std::tuple<int, int, int> randint3(int l, int r, uint64_t rng_val)
{
    const uint64_t d = static_cast<uint64_t>(r - l);
    const uint64_t v1 = rng_val >> 42;
    const uint64_t v2 = (rng_val >> 21) & 0x1FFFFFULL;
    const uint64_t v3 = rng_val & 0x1FFFFFULL;

    return {
        l + static_cast<int>((v1 * d) >> 22),
        l + static_cast<int>((v2 * d) >> 21),
        l + static_cast<int>((v3 * d) >> 21)
    };
}

} // namespace library
// ============== End of library/rng.hpp ==============


using namespace std;

#define rep1(a) for (int _ = 0; _ < (a); ++_)
#define rep2(i, a) for (int i = 0; i < (a); ++i)
#define rep3(i, a, b) for (int i = a; i < (b); ++i)
#define rrep1(a) for (int i = (a) - 1; i >= 0; --i)
#define rrep2(i, a) for (int i = (a) - 1; i >= 0; --i)
#define rrep3(i, a, b) for (int i = (b) - 1; i >= (a); --i)
#define overload3(a, b, c, d, ...) d
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload3(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define in(i, vec) for (auto i : (vec))
template <class T>
using pqueue = priority_queue<T, vector<T>>; // 大きい順
template <class T>
using pqueue_g = priority_queue<T, vector<T>, greater<T>>; // 小さい順
using i8=int8_t; using i16=int16_t; using i32=int32_t; using i64=int64_t;
using u8=uint8_t; using u16=uint16_t; using u32=uint32_t; using u64=uint64_t;

library::Timer timer;
uniform_real_distribution<double> rnd(0.0, 1.0);
normal_distribution<double> randn(0.0, 1.0);
library::xoshiro256 rng(12345);
// ============== End of define.hpp ==============


#include <atcoder/fenwicktree>

// ============== library/sa.hpp ==============
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <random>
#include <type_traits>

namespace library{

template<bool TimeBased = true>
struct SAScheduler {
private:
    using Clock = std::chrono::steady_clock;
    using Limit = std::conditional_t<TimeBased, double, uint64_t>;

    inline static double log_table[65536];
    Clock::time_point start;
    double t1, t2, b, temperature;
    Limit limit;
    uint64_t itr = 0, freq;

    bool update() {
        double progress;
        if constexpr (TimeBased) {
            double elapsed = std::chrono::duration<double>(Clock::now() - start).count();
            if (elapsed >= limit) return false;
            progress = elapsed / limit;
        } else {
            if (itr >= limit) return false;
            progress = limit > 1 ? double(itr) / double(limit - 1) : 0.0;
        }

        double t_exp = t1 * std::pow(t2 / t1, progress);
        double t_lin = t1 + (t2 - t1) * progress;
        temperature = std::pow(t_exp, b) * std::pow(t_lin, 1.0 - b);
        return true;
    }

public:
    // limit_: 時間ベースなら秒数、回数ベースなら反復回数
    SAScheduler(double start_temp_, double end_temp_, Limit limit_,
              double balance_ = 0.5, uint64_t update_freq_ = 256, uint64_t seed = 99999)
        : t1(start_temp_), t2(end_temp_), b(balance_), temperature(t1),
          limit(limit_), freq(std::max(uint64_t(1), update_freq_)) {
        for (int i = 0; i < 65536; ++i) log_table[i] = std::log((1.0 + i) / 65536);
        std::mt19937_64 shuffle_rng(seed);
        std::shuffle(log_table, log_table + 65536, shuffle_rng);
        if constexpr (TimeBased) start = Clock::now();
    }

    struct Sentinel {};

    struct Iterator {
        SAScheduler* s;
        bool running;
        uint64_t remaining;

        double operator*() const {
            return s->temperature * log_table[s->itr & 65535];
        }

        Iterator& operator++() {
            ++s->itr;

            if constexpr (!TimeBased) {
                if (s->itr >= s->limit) {
                    running = false;
                    return *this;
                }
            }

            bool refresh = (--remaining == 0);
            if constexpr (!TimeBased) {
                // 最後の反復では終了温度を使う
                refresh |= (s->itr == s->limit - 1);
            }
            if (refresh) {
                running = s->update();
                remaining = s->freq;
            }
            return *this;
        }

        bool operator!=(Sentinel) const { return running; }
    };

    Iterator begin() { return {this, update(), freq}; }
    Sentinel end() const { return {}; }

    uint64_t iterations() const { return itr; }
    double current_temperature() const { return temperature; }
};

} // namespace library

// ============== End of library/sa.hpp ==============



constexpr int N=64;
constexpr int M=256;
array<int,M> A;

void get_input(){
    int n; cin>>n;assert(n==N);
    rep(i,M){
        int a; cin>>a;A[i]=a-1;
    }
}

using Board = array<array<int,N>,N>;
// ULDR;
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};

char BOARD_TO_CHAR[4]={'0','2','@','1'};

tuple<int,int,int> mid_to_pos(int i){
    if(i<N){
        return{0,i,2};
    }else if(i<2*N){
        return{i-N,N-1,1};
    }else if(i<3*N){
        return{N-1,N-1-(i-2*N),0};
    }else{
        return {N-1-(i-3*N),0,3};
    }
};
int pos_to_mid(int x, int y, int d){
    if(d==2) return y;
    else if(d==1) return x+N;
    else if(d==0) return (N-1-y)+2*N;
    else return (N-1-x)+3*N;
}

array<int,M> get_B(const Board& board){
    array<int,M> B; B.fill(-1);
    rep(i,M){
        int a = A[i];
        auto [x,y,d] = mid_to_pos(i);
        while((unsigned)x<N && (unsigned)y<N){
            d^=board[x][y];
            x+=dx[d];
            y+=dy[d];
        }
        B[pos_to_mid(x,y,d^2)]=a;
    }
    return B;
};

constexpr u32 ADD_MASK[4]={
    0x01010100u, 0x01010000u,0x01000000u,0x00000000u
};

struct BIT256{
    u64 x[4]={};
    u32 y=0;

    int sum_add(int b){
        int i = b>>6, j=b&63;
        int sum = (y>>(8*i)&255) + __builtin_popcountll(x[i]&((1ULL<<j)-1));
        x[i] |= 1ULL << j;
        y += 0x01010100u << (8*i);

        return sum;
    }
};

struct State{
    Board board;
    array<u8,4> lid[N][N];
    array<int,M> B;

    void compute_light(int i){
        int a = A[i];
        auto [x,y,d] = mid_to_pos(i);
        while((unsigned)x<N && (unsigned)y<N){
            lid[x][y][d]=i;
            d^=board[x][y];
            x+=dx[d];
            y+=dy[d];
        }
        B[pos_to_mid(x,y,d^2)]=a;
    }

    State(const Board& __board):board(__board){
        rep(i,M) compute_light(i);
    }

    int get_score(){
        atcoder::fenwick_tree<int> bit(M);
        int score = 0;
        rep(i,M){
            int b = B[i];
            score += bit.sum(0,b);
            bit.add(b,1);
        }
        return score;
    }

    void change_board(int x,int y, int c){
        board[x][y]=c;
        auto pre = lid[x][y];
        for(int i:pre) compute_light(i);
    }
};

int get_score(const array<int,M>& B){
    // atcoder::fenwick_tree<int> bit(M);
    BIT256 bit{};
    int score = 0;
    rep(i,M){
        int b = B[i];
        // score += bit.sum(0,b);
        // bit.add(b,1);
        score += bit.sum_add(b);
    }
    return score;
};

void output(const Board& board){
    string out;
    rep(i,N)rep(j,N){
        out+=BOARD_TO_CHAR[board[i][j]];
        if(j+1==N)out+='\n';
    }
    cout<<out<<flush;
}


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

    get_input();

    State state(Board{});
    int score = state.get_score();
    int best_score = score;
    State best_state = state;

    cerr << "initial score " << score << endl;

    library::SAScheduler<true> sa(1e0,1e-2,1.95,0.8);

    int itr = 0;

    const int CM = 5;

    for(double add:sa){
        ++itr;
        int x = rng()%N, y=rng()%N, c=rng()%CM; if(c!=1 && c!=3)c=0;
        while(state.board[x][y]==c){
            x = rng()%N, y=rng()%N, c=rng()%CM; if(c!=1 && c!=3)c=0;
        }
        int pc = state.board[x][y];
        state.change_board(x,y,c);
        int new_score = state.get_score();

        if(new_score >= score+add){
            score = new_score;
            if(score > best_score){
                best_score = score;
                best_state = state;
            }
        }else{
            state.change_board(x,y,pc);
        }
    }

    output(best_state.board);
    cerr << "best score " << best_score << endl;
}
// ============== End of main.cpp ==============



0