結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー arfz
提出日時 2026-11-23 22:09:47
言語 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  
実行時間 301 ms / 2,000 ms
+ 64µs
コード長 11,014 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,780 ms
コンパイル使用メモリ 447,148 KB
実行使用メモリ 9,920 KB
スコア 1,383,237
最終ジャッジ日時 2026-11-23 22:10:12
合計ジャッジ時間 23,701 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

//#pragma GCC optimize("O3,inline")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC target("movbe")
//#pragma GCC target("aes,pclmul,rdrnd")

#ifdef IS_LOCAL
#include"template.hpp"
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef ATCODER
#pragma GCC target("arch=skylake-avx512")
#endif
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef ATCODER
#pragma GCC target("arch=skylake-avx512")
#endif
#include<utility>
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<tuple>
#include<chrono>
#include<map>
#include<set>
#include<algorithm>
#include<queue>
#include<random>
#include<numeric>
#include<functional>
#include<stack>
#include<cassert>
#include<cmath>
#include<cstring>
#include<unordered_set>
#include<unordered_map>
#include<ranges>
#include<bitset>
#include<bit>
#include<cstdlib>
#include<memory>
#include<stdexcept>
#include<array>
#ifdef __GNUG__
#include <immintrin.h>
#endif
#endif

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define reps(i, s, n) for (int i = s; i < (int)n; ++i)
#define repv(x, v) for (const auto& x : v)
#define rep2(i,j,n) for (int i = 0; i < (int)n; ++i) for (int j = 0; j < (int)n; ++j)
#define rep3(i,j,k,n) for (int i = 0; i < (int)n; ++i) for (int j = 0; j < (int)n; ++j) for (int k= 0; k < (int)n; ++k)
#define all(v) v.begin(),v.end()

template<class T, std::size_t N>
struct mvec_impl { using type = std::vector<typename mvec_impl<T, N - 1>::type>; };
template<class T>
struct mvec_impl<T, 1> { using type = std::vector<T>; };
template<class T, std::size_t N = 1>
using vec = typename mvec_impl<T, N>::type;
template<class T>
auto make_vec(size_t n, T x) { return vec<T>(n, x); }
template<class T, class... Args>
auto make_vec(size_t n, Args... args) { return std::vector(n, make_vec<T>(args...)); }
using ull = unsigned long long;
using uint = unsigned int;
using ushort = unsigned short;
using uchar = unsigned char;
using ll = long long;
template<typename T>
using t2 = std::pair<T, T>;
template<typename T>
using t3 = std::tuple<T, T, T>;
using i2 = t2<int>;
using i3 = t3<int>;
using clk = std::chrono::steady_clock;
// global

constexpr int N = 64;
constexpr int N2 = N*N;
constexpr int M = 4 * N;
vec<int> A;

#ifdef IS_LOCAL
constexpr bool local = true;
#else
constexpr bool local = false;
#endif

std::map<std::string, std::string> LOG;

std::random_device rnd;
std::mt19937 mt(rnd());
std::uniform_real_distribution<> rand01(0.0, 1.0);

clk::time_point start_time;
std::ofstream ofstr; // result file

// utils

template<class T>
concept StringLike = requires(T x) { {std::string_view(x) }; };
template<class T>
concept TupleLike = requires {
    typename std::tuple_size<std::remove_cvref_t<T>>::type;
};
template<class T>
concept Range = std::ranges::range<T> && !StringLike<T> && !TupleLike<T>;
void print(const TupleLike auto&);
void print(const Range auto&);
void print(const auto&);
void print(const TupleLike auto& tp) {
    std::cerr << "(";
    size_t i = 0;
    std::apply([&](auto&&... args){
        ((std::cerr << (i++ ? " " : ""), print(args)), ...);
    }, tp);
    std::cerr << ")";
}
void print(const Range auto& r) {
    std::cerr << "[";
    bool f = true;
    for (auto&& x : r) {
        if (!std::exchange(f, false)) std::cerr << " ";
        print(x);
    }
    std::cerr << "]";
}
void print(const auto& x) {
    std::cerr << x;
}

// ---- debug ----
template<typename... Ts>
//void dprint_impl(std::string_view names, const Ts&... xs) {
void dprint(const Ts&... xs) {
    if (!local) return;
    std::cerr << "# ";// << names << ": ";
    ((print(xs), std::cerr << " "), ...);
    std::cerr << '\n';
}
//#define dprint(...) dprint_impl(#__VA_ARGS__, __VA_ARGS__)

double getElapsed(const clk::time_point& start){
    return (std::chrono::duration<double>(clk::now()-start)).count();
}

template<typename T>
T sum(const vec<T>& v){
    return std::accumulate(all(v), T{});
}

template<class T>
auto argmax(const vec<T>& v) {
    auto it = std::ranges::max_element(v);
    return std::pair{size_t(it - v.begin()), *it};
}

template<class T>
auto argmin(const vec<T>& v) {
    auto it = std::ranges::min_element(v);
    return std::pair{size_t(it - v.begin()), *it};
}

vec<int> range(const int n){
    vec<int> v(n);
    std::iota(all(v), 0);
    return v;
}

uint64_t xorShift64() {
    static uint64_t x = 88172645463325252ULL;
    //static uint64_t y = 1315423911ULL;

    //uint64_t t = x;
    //x = y;
    //t ^= t << 23;
    //t ^= t >> 17;
    //t ^= y ^ (y >> 26);
    //y = t;
    //return t + y;
    x ^= x << 7ull;
    x ^= x >> 9ull;
    return x;
}

double rand01fast(){
    uint64_t x = xorShift64();
    return (x >> 11 ) * (1.0 / (1ULL << 53));
}

uint32_t xorShift() {
  static uint32_t y = 2463534242;
  y = y ^ (y << 13); y = y ^ (y >> 17);
  return y = y ^ (y << 5);
}

uint32_t randint(const uint32_t size){
    uint32_t a = xorShift();
    uint64_t m = (uint64_t)a * (uint64_t) size;
    return m >> 32;
}

template<typename T>
T choice(const vec<T>& v){
    return v[randint(v.size())];
}

vec<int> sample(int mx, int num){
    vec<int> res;
    while(res.size() < num){
        res.emplace_back(randint(mx));
        std::sort(all(res));
        auto unique_end = std::unique(all(res));
        res.erase(unique_end, res.end());
    }
    return res;
}

bool randbool(){
    return xorShift() & 1;
}

template<typename T>
vec<int> argsort(const vec<T>& v){
    auto res = range(v.size());
    std::ranges::sort(res, {}, [&](int i){return v[i];});
    return res;
}

// functions
constexpr int LOG_TABLE_SIZE = 1 << 14;
double log_table[LOG_TABLE_SIZE];
inline double log_rand() {
    static int idx = 0;
    if (idx == LOG_TABLE_SIZE) {
        idx = 0;
    }
    return log_table[idx++];
}

void initialize(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    if(local){
        ofstr = std::ofstream("./result.txt");
        auto original_buf = std::cout.rdbuf();
        std::cout.rdbuf(ofstr.rdbuf());
    }
    start_time = clk::now();
    for (int i = 0; i < LOG_TABLE_SIZE; i++) {
        log_table[i] = log(rand01(mt));
    }

    int n;
    std::cin >> n;

    rep(m,M){
        int a;
        std::cin >> a;
        A.push_back(a);
    }
}
void deconstruct(){
}

using Board = vec<int>;

int reflect(int m, int d){
    if(m==1){
        switch(d){
            case 0: return 1;
            case 1: return 0;
            case 2: return 3;
            case 3: return 2;
        }
    } else {
        switch(d){
            case 0: return 3;
            case 1: return 2;
            case 2: return 1;
            case 3: return 0;
        }
    }
    throw std::runtime_error("ERROR");
}

// Thanks for https://kopricky.github.io/code/Computation_Advanced/inversion_count.html

template<typename T> class BIT {
private:
int n;
vec<T> bit;
public:
void add(int i, T x){
i++;
while(i < n){
bit[i] += x, i += i & -i;
}
}
T sum(int i){
i++;
T s = 0;
while(i > 0){
s += bit[i], i -= i & -i;
}
return s;
}
BIT(){}
BIT(int sz) : n(sz+1), bit(n, 0){}
BIT(const vec<T>& v) : n((int)v.size()+1), bit(n, 0){
for(int i = 0; i < n-1; i++){
add(i,v[i]);
}
}
};
 
long long inv_count(const Board& u)
{
int n = (int)u.size();
BIT<int> bt(n);
long long ans = 0;
for(int i = 0; i < n; i++){
ans += i - bt.sum(u[i]);
bt.add(u[i], 1);
}
return ans;
}
 
vec<int> simulate(const Board& board){
    const vec<i2> dir{{1,0},{0,-1},{-1,0},{0,1}};
    vec<int> B(M);
    rep(d,4){
        rep(n,N){
            int td = d;
            int a = A[d*N+n];
            int x,y;
            switch(d){
                case 0:{
                           x = 0; y = n; break;
                       }
                case 1:{
                           x = n; y = N-1; break;
                       }
                case 2:{
                           x = N-1; y = N-1-n; break;
                       }
                case 3:{
                           x = N-1-n; y = 0; break;
                       }
            }
            bool in_board = true;
            while(in_board){
                int m = board[N*x+y];
                if(m != 0){
                    td = reflect(m, td);
                }
                const auto& [dx,dy] = dir[td];
                x += dx;
                y += dy;
                if(x < 0){
                    B[y] = a;
                    in_board = false;
                }
                if(N <= x){
                    B[N*2+N-1-y] = a;
                    in_board = false;
                }
                if(y < 0){
                    B[N*3+N-1-x] = a;
                    in_board = false;
                }
                if(N <= y){
                    B[N+x] = a;
                    in_board = false;
                }
            }
        }
    }
    return B;
}

Board solve(){

    auto bboard = Board(N2,0);

    auto B = simulate(bboard);
    int bscore = inv_count(B);
    int nscore = bscore;
    auto nboard = bboard;

    const double stemp = 1.0;
    const double etemp = 0;
    double elapsed = getElapsed(start_time);
    constexpr double end_time = 0.3;
    
    int subCnt = 0;
    int loopCnt = 0;
    int naccept = 0;
    int gaccept = 0;
    double temp = stemp;

    while(elapsed < end_time){
        subCnt++;
        if(subCnt == 10){
            subCnt = 0;
            elapsed = getElapsed(start_time);
            temp = stemp + elapsed * (etemp - stemp) / end_time;
        }


        int x = randint(N);
        int y = randint(N);
        int v = N*x+y;
        int ps = nboard[v];
        int s = randint(3);

        if(ps == s){
            s = (s+1)%3;
        }
        nboard[v] = s;

        auto tB = simulate(nboard);
        double tscore = inv_count(tB);
        //dprint(tB,tscore);
        //return nboard;


        const double diff = nscore - tscore;

        bool accept = false;
        if(diff > 0){
            accept = true;
        } else if(diff <= 0){
            if(log_rand() * temp < diff){
                accept = true;
            }
        }
        if(accept){
            naccept++;
            nscore = tscore;
            if(nscore < bscore){
                gaccept++;
                dprint("bscore:", tscore, elapsed);
                bscore = nscore;
                bboard = nboard;
            }
        } else {
            nboard[v] = ps;
        }
        loopCnt++;
    }
    dprint("perm loopCnt:", loopCnt);
    dprint("perm bscore:", bscore);

    return bboard;
}

// main

int main(){
    initialize();
    auto board = solve();
    rep(x,N){
        rep(y,N){
            int v = N*x+y;
            std::cout << board[v];
        }
        std::cout << std::endl;
    }
    if(local){
        double elapsed = getElapsed(start_time);
        dprint("time:", elapsed);
    }
    deconstruct();
    return 0;
}
0