結果

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

ソースコード

diff #
raw source code

// (◕ᴗ◕✿)

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, n) for (ll i = s; i < (n); ++i)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = 3.141592653589793;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;

#define debug(var) do { cerr << #var << " :\n"; view(var); } while(0)
template<typename T>void view(const T& e) {cerr << e;}
template<typename T1, typename T2>void view(const pair<T1, T2>& p) {cerr << "{" << p.first << ", " << p.second << "}";}
template<typename T>void view(const vc<T>& v) {for (const auto& e : v) {view(e);cerr << " ";} cerr << endl;}
template<typename T>void view(const vv<T>& vv) {for (const auto& v : vv) {view(v);} cerr << endl;}
template<typename T>void view(const set<T>& s) {for (const auto& e : s) {view(e);cerr << " ";} cerr << endl;}
template<typename T>void view(const multiset<T>& s) {for (const auto& e : s) {view(e);cerr << " ";} cerr << endl;}
template<typename T>void view(const unordered_set<T>& s) {for (const auto& e : s) {view(e);cerr << " ";} cerr << endl;}
template<typename T1, typename T2>void view(const map<T1, T2>& mp){for (const auto& e : mp) {view(e);cerr << " ";} cerr << endl;}

unsigned int randxor(){
    static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned int t;
    t = (x ^ (x << 11)); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
int randint(int a, int b) {return(a + randxor() % (b - a));}

struct Timer {
    public:
        Timer(int limit){
            start = chrono::high_resolution_clock::now();
            goal = start + chrono::milliseconds(limit);
        }

        inline double rate(){
            return (chrono::high_resolution_clock::now() - start).count() / (double)(goal - start).count();
        }

        inline int get_time(){return (chrono::high_resolution_clock::now() - start).count() / 1e6;}

    private:
        chrono::high_resolution_clock::time_point start;
        chrono::high_resolution_clock::time_point goal;  
};

double log_table[70000];
//variable
constexpr int TIME_LIMIT = 190;
constexpr int N = 64;
constexpr int M = 4 * N;
int A[M];

constexpr int T = 66;
int d[4] = {-T, 1, T, -1};
int md[3][4] = {
    {0, 1, 2, 3},
    {1, 0, 3, 2},
    {3, 2, 1, 0}
};
namespace simulated_annealing {

constexpr int transition_num = 2;
int transition_count[transition_num];
int success_count[transition_num];

using Cost = int;

struct Config {
    int time_limit;
    int temperature_type;
    double start_temp, goal_temp;
    int probability[transition_num];
};

struct State {
    Cost score;
    array<int, T * T> G;
    array<array<int, 4>, T * T> to;
    array<int, M> P, rP;
    State(){
        score = inf;
    }

    // void operator=(const State &other) {
    //     score = other.score;
    // }

    void update(int v, int k) {
        int sv = v, sk = k;
        int turn = 0;
        while (true){
            turn++;
            int nv = v + d[k];
            if (G[nv] == -1) return;
            int nxtk = md[G[nv]][k];
            to[nv][nxtk] = to[v][k];
            v = nv;
            k = nxtk;
            if (v == sv && k == sk){
                rep(i, turn + 1){
                    int nv = v + d[k];
                    int nxtk = md[G[nv]][k];
                    to[nv][nxtk] = -1;
                    v = nv;
                    k = nxtk;
                }
                break;
            }
        }
    }

    void init(){
        srep(i, 1, T - 1){
            G[i] = -1;
            P[3 * N - i] = A[i - 1];
            rP[A[i - 1]] = 3 * N - i;
            to[i][2] = A[i - 1];
            
            G[i * T] = -1;
            P[N + i - 1] = A[4 * N - i];
            rP[A[4 * N - i]] = N + i - 1;
            to[i * T][1] = A[4 * N - i];
            
            G[i * T + T - 1] = -1;
            P[4 * N - i] = A[i - 1 + N];
            rP[A[i - 1 + N]] = 4 * N - i;
            to[i * T + T - 1][3] = A[i - 1 + N];
            
            G[(T - 1) * T + i] = -1;
            P[i - 1] = A[3 * N - i];
            rP[A[3 * N - i]] = i - 1;
            to[(T - 1) * T + i][0] = A[3 * N - i];
        }
        srep(i, 1, T - 1) srep(j, 1, T - 1) G[i * T + j] = 0;
        srep(i, 1, T - 1){
            update(i, 2);
            update(i * T, 1);
            update(i * T + T - 1, 3);
            update((T - 1) * T + i, 0);
        }
        score = CalcScore();
    }

    Cost CalcScore(Cost threshold = inf){
        bitset<M> nw;
        Cost ret = 0;
        rep(i, M){
            ret += (nw >> P[i]).count();
            nw.set(P[i]);
        }
        return ret;
    }

    void transition01(Cost &threshold){
        transition_count[0]++;
        int i = randint(1, T - 1), j = randint(1, T - 1);
        int v = i * T + j;
        int x = randint(0, 3);
        if (G[v] == x) return;
        if ((G[v] ^ x) == 1){
            if (to[v][2] != -1 && to[v][3] != -1) swap(P[rP[to[v][2]]], P[rP[to[v][3]]]);
            if (to[v][0] != -1 && to[v][1] != -1) swap(P[rP[to[v][0]]], P[rP[to[v][1]]]);
        }else if ((G[v] ^ x) == 2){
            if (to[v][0] != -1 && to[v][3] != -1) swap(P[rP[to[v][0]]], P[rP[to[v][3]]]);
            if (to[v][1] != -1 && to[v][2] != -1) swap(P[rP[to[v][1]]], P[rP[to[v][2]]]);
        }else if ((G[v] ^ x) == 3){
            if (to[v][0] != -1 && to[v][2] != -1) swap(P[rP[to[v][0]]], P[rP[to[v][2]]]);
            if (to[v][1] != -1 && to[v][3] != -1) swap(P[rP[to[v][1]]], P[rP[to[v][3]]]);
        }
        auto myscore = CalcScore();
        if (myscore <= threshold){
            success_count[0]++;
            score = myscore;
            if ((G[v] ^ x) == 1){
                swap(to[v][2], to[v][3]);
                swap(to[v][0], to[v][1]);
                if (to[v][2] != -1 && to[v][3] != -1) swap(rP[to[v][2]], rP[to[v][3]]);
                if (to[v][0] != -1 && to[v][1] != -1) swap(rP[to[v][0]], rP[to[v][1]]);
            }else if ((G[v] ^ x) == 2){
                swap(to[v][0], to[v][3]);
                swap(to[v][1], to[v][2]);
                if (to[v][0] != -1 && to[v][3] != -1) swap(rP[to[v][0]], rP[to[v][3]]);
                if (to[v][1] != -1 && to[v][2] != -1) swap(rP[to[v][1]], rP[to[v][2]]);
            }else if ((G[v] ^ x) == 3){
                swap(to[v][0], to[v][2]);
                swap(to[v][1], to[v][3]);
                if (to[v][0] != -1 && to[v][2] != -1) swap(rP[to[v][0]], rP[to[v][2]]);
                if (to[v][1] != -1 && to[v][3] != -1) swap(rP[to[v][1]], rP[to[v][3]]);
            }
            G[v] = x;
            rep(k, 4) if (to[v][k] != -1) update(v, k);
        }else{
            if ((G[v] ^ x) == 1){
                if (to[v][2] != -1 && to[v][3] != -1) swap(P[rP[to[v][2]]], P[rP[to[v][3]]]);
                if (to[v][0] != -1 && to[v][1] != -1) swap(P[rP[to[v][0]]], P[rP[to[v][1]]]);
            }else if ((G[v] ^ x) == 2){
                if (to[v][0] != -1 && to[v][3] != -1) swap(P[rP[to[v][0]]], P[rP[to[v][3]]]);
                if (to[v][1] != -1 && to[v][2] != -1) swap(P[rP[to[v][1]]], P[rP[to[v][2]]]);
            }else if ((G[v] ^ x) == 3){
                if (to[v][0] != -1 && to[v][2] != -1) swap(P[rP[to[v][0]]], P[rP[to[v][2]]]);
                if (to[v][1] != -1 && to[v][3] != -1) swap(P[rP[to[v][1]]], P[rP[to[v][3]]]);
            }
        }
    }
};

State simulated_annealing(const Config& config, State state) {
    State best;
    Timer timer(config.time_limit);
    int roop = 0;
    double tmp = config.start_temp;
    double rate = 0;
    while (true){
        roop++;
        int randomint = randint(0, 100);
        Cost threshold = state.score - tmp * log_table[randint(0, 70000)];

        // if (randomint < config.probability[0]){ // transition 01
        //     state.transition01(threshold);
        // }else{
        // }
        state.transition01(threshold);

        if (rate > 0.0 && best.score > state.score){ // update best
            best = state;
        }

        if (roop % 1000 == 0){
            // if (roop % 10000 == 0) cerr << state.score << " " << best.score << endl;
            rate = timer.rate();
            if (config.temperature_type == 0){
                tmp = config.start_temp + rate * (config.goal_temp - config.start_temp);
            }else{
                tmp = config.start_temp * pow(config.goal_temp / config.start_temp, rate);
            }
            if (rate > 1.0){
                break;
            }
        }
    }
    cerr << "roop : " << roop << endl;
    cerr << "score : " << best.score << endl;
    rep(i, transition_num) cerr << "transition" << i << " conversion rate : " << fixed << setprecision(4) << success_count[i] / (double)transition_count[i] * 100 << " %  " << success_count[i] << " / " << transition_count[i] << endl;
    return best;
};

}// namespace simulated_annealing

struct Solver{
    simulated_annealing::State ans;
    void input(){
        int a; cin >> a;
        rep(i, M) cin >> A[i];
        rep(i, M) A[i]--;
    }

    void init(){
		double n = 1 / (double)(2 * 70000);
		for (int i = 0; i < 70000; i++) {
			log_table[i] = log(((double)i / 70000) + n);
		}
    }

    void output(){
        srep(i, 1, T - 1){
            srep(j, 1, T - 1) cout << ans.G[i * T + j];
            cout << '\n';
        }
    }

    void run(){
        Timer timer(TIME_LIMIT);
        input();
        init();
        simulated_annealing::State state;
        state.init();
        simulated_annealing::Config config = {
            TIME_LIMIT - timer.get_time(),
            0,
            0.5,
            0.01,
            {70, 100}
        };
        ans = simulated_annealing::simulated_annealing(config, state);
        cerr << "score : " << ans.CalcScore() << endl;
        output();
    }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    Solver solver;
    solver.run();
    return 0;
}
0