結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー bird01
提出日時 2026-11-23 22:19:06
言語 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  
実行時間 1,915 ms / 2,000 ms
+ 731µs
コード長 6,768 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,016 ms
コンパイル使用メモリ 254,800 KB
実行使用メモリ 9,908 KB
スコア 1,415,683
最終ジャッジ日時 2026-11-23 22:20:55
合計ジャッジ時間 104,698 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge2_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// 5.
#include <algorithm>
#include <cmath>
#include <iostream>
#include <random>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <chrono>

#define rep(i,a,b) for(int i=a;i<b;i++)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)((x).size())
#define fo(x) cout<<x<<endl
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
template <typename T> bool chmin(T &a, const T& b){if(a>b){a=b; return true;}return false;}
template <typename T> bool chmax(T &a, const T& b){if(a<b){a=b; return true;}return false;}
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
//const long long INF=1e18;
const int INF=1000000000;

const int64_t CYCLES_PER_SEC = 2900000000;//3000000000;
const double TIMELIMIT = 1.95;
constexpr int RANDOM_SEED = 42; // 乱数のシード値

const int dx[]={-1,0,1,0}; // 上,左,下,右
const int dy[]={0,-1,0,1};

bool is_outer_range(int R1,int r1,int R2,int r2){
  return (r1<0 or R1<=r1 or r2<0 or R2<=r2);
}

mt19937 mt(RANDOM_SEED);
uniform_int_distribution<> rand3(0,2);    // [0,2]を出力
uniform_int_distribution<> rand64(0,63);    // [0,2]を出力

struct Input{
  int N;
  vi A;
  Input(int N=0, vi A={}): N(N), A(A) {}
};
struct Output{
  vs S;
  Output(vs S={}): S(S) {}
};

struct Solver {
  // グローバル変数==============================
  std::chrono::system_clock::time_point start_time;
  Input input;
  Output best_ans;
  ll best_score;

  // 便利関数==============================
  // 乱数
  double Randouble(){
    return 1.0 * rand() / RAND_MAX;
  }
  // スコア計算
  ll calc_score(const vector<int>& a, const vector<string>& board) {
    int n = board.size();
    int m = 4 * n;

    // b[i]: 出口iに到達したレーザーの値
    vector<int> b(m, 0);

    // 上=0、右=1、下=2、左=3
    const int dr[4] = {-1, 0, 1, 0};
    const int dc[4] = {0, 1, 0, -1};

    // 各入口からレーザーを追跡
    rep(i,0,m) {
      int r, c, d;

      // 入口の座標と進行方向
      if (i < n) {
        r = 0;
        c = i;
        d = 2;
      } else if (i < 2 * n) {
        r = i - n;
        c = n - 1;
        d = 3;
      } else if (i < 3 * n) {
        r = n - 1;
        c = 3 * n - 1 - i;
        d = 0;
      } else {
        r = 4 * n - 1 - i;
        c = 0;
        d = 1;
      }

      // レーザーを進める
      rep(step,0,4*n*n) {
        char cell = board[r][c];

        // 鏡による反射
        if (cell == '1') d ^= 1;
        if (cell == '2') d ^= 3;

        // 次のマスへ
        r += dr[d];
        c += dc[d];

        // 盤面から出た場合
        if (r < 0 || r >= n || c < 0 || c >= n) {
          int exit;

          if (r < 0) {
            exit = c;
          } else if (c >= n) {
            exit = n + r;
          } else if (r >= n) {
            exit = 3 * n - 1 - c;
          } else {
            exit = 4 * n - 1 - r;
          }

          b[exit] = a[i];
          break;
        }
      }
    }

    // 転倒数を計算
    ll inversions = 0;
    rep(i,0,m) {
      rep(j,i+1,m) {
        if (b[i] > b[j]) {
          inversions++;
        }
      }
    }

    // 得点 = 全ペア数 - 転倒数
    // ここで返すのは転倒数にする
    ll score = inversions;

    return score;
  }
  // 初期解を生成する
  Output get_initial_solution(){
    vs S(input.N,string(input.N,'0'));
    return {S};
  }
  // pre_ansからnew_ansを作る(ランダム変更)
  Output make_new_ans(Output &pre_ans){
    // コピー
    Output new_ans=pre_ans;

    // 隣接を求める
    int row=rand64(mt);
    int col=rand64(mt);
    int nx=rand3(mt);
    while(nx==new_ans.S[row][col]-'0') nx=rand3(mt);
    new_ans.S[row][col]=nx+'0';

    return new_ans;
  }

  // 初期解生成
  void init(){
    Output ans=get_initial_solution();
    best_ans=ans;
    best_score=calc_score(input.A, best_ans.S);
  }
  
  void SA(){
    Output pre_ans=best_ans;
    best_score = calc_score(input.A, pre_ans.S);
    ll pre_score = best_score;
    
    double kankaku = TIMELIMIT / 10 * 1000; // msオーダー
    double ne = 0;
    int cnt = 0;
    int cnt2 = 0;

    double start_temp = 8;
    double end_temp = 0;
    
    while(1){
      // 時間計測
      std::chrono::system_clock::time_point end = std::chrono::system_clock::now();  // 計測終了時間
      double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end-start_time).count(); //処理に要した時間をミリ秒に変換
      if(TIMELIMIT*1000 < elapsed) break;

      double temp = start_temp + (end_temp - start_temp) * elapsed / TIMELIMIT / 1000;

      if (elapsed> ne) {
        ne += kankaku;
        cerr << temp << " " << pre_score << " " << best_score << endl;
      }

      // // 新たな解の生成==============================
      // Output new_ans;
      // int w=rand10000(mt)%2;
      // if(w==0){
      //   new_ans=make_new_ans1(pre_ans);
      // }else{
      //   new_ans=make_new_ans2(pre_ans);
      // }
      Output new_ans=make_new_ans(pre_ans);

      // new_scoreを計算
      ll new_score=calc_score(input.A, new_ans.S); // スコア計算
      
      // 採用するか否かを決定(scoreは小さいほど良い)
      // if(new_score<best_score){
      //   swap(best_ans,new_ans);
      // }

      double prob = exp((pre_score-new_score)/temp);
      
      if (prob*10000 > 10000 * (rand() % 10000)) {
        pre_score=new_score;
        pre_ans=new_ans;

        if(chmin(best_score,new_score)){
          best_score=new_score;
          best_ans=new_ans;
        }
        cnt2++;
      }
      cnt++;
    }
    
    // dump(cnt);
		// dump(cnt2);
    //ti = timer.get();
    //cerr << ti << endl;
  }
  
  void input_func() {
    start_time = std::chrono::system_clock::now(); // 計測開始時間
    
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M;
    cin >> N;
    M=N*4;
    vi A(M);
    rep(i,0,M) cin >> A[i];
    input={N,A};
  }

  int solve() {
    init();
    SA();
    return 0;
  }
  
  void output(){
    cerr << best_score << endl;
    // cerr << round(1000000LL*1000000LL/(best_score+1)) << endl;
    rep(i,0,input.N) cout << best_ans.S[i] << endl;

    // デバッグ
    // double ti = timer.get();
    // cerr << ti-start_time << endl;
  }
};

Solver solver;
int main(int argc, char *argv[]) {
  solver.input_func();
  solver.solve();
  solver.output();
  return 0;
}
0