結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー Shun_PI
提出日時 2026-11-23 21:56:02
言語 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,898 ms / 2,000 ms
+ 255µs
コード長 4,855 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,396 ms
コンパイル使用メモリ 392,392 KB
実行使用メモリ 9,872 KB
スコア 1,457,811
最終ジャッジ日時 2026-11-23 21:57:58
合計ジャッジ時間 105,198 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge2_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <string>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
constexpr int MOD = 1000000007;
vector<lint> RH_B = {1532834020, 1388622299};
vector<lint> RH_M = {2147482409, 2147478017};
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }
template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }
int N, M;
vector<int> A;
vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, 1, 0, -1};
vector<char> dc = {'U', 'R', 'D', 'L'};
mt19937 mt(0);

void set_B(int idx, vector<int> &B, vector<int> &light_id) {
  if(idx < N) {
    B[idx] = light_id[0*N*4 + idx*4 + 0];
  } else if(idx < 2*N) {
    B[idx] = light_id[(idx-N)*N*4 + (N-1)*4 + 1];
  } else if(idx < 3*N) {
    B[idx] = light_id[(N-1)*N*4 + (N-1 - (idx-2*N))*4 + 2];
  } else if(idx < 4*N) {
    B[idx] = light_id[(N-1 - (idx-3*N))*N*4 + 0*4 + 3];
  }
}

int calc_score(vector<int> &B) {
  //転倒数を最小化
  int score = M*(M-1) / 2;
  REP(i, M) FOR(j, i+1, M) if(B[i] > B[j]) score--;
  return score;
}
vector<int> get_B(vector<int> &light_id, vector<string> &grid) {
  vector<int> B(M);
  //全てのポートから発射される光を順番に追跡する(計算量N*N*4)
  REP(port, M) {
    int x, y, d;
    if(port < N) {
      x = 0;
      y = port;
      d = 2;
    } else if(port < 2*N) {
      x = port - N;
      y = N-1;
      d = 3;
    } else if(port < 3*N) {
      x = N-1;
      y = N-1 - (port - 2*N);
      d = 0;
    } else {
      x = N-1 - (port - 3*N);
      y = 0;
      d = 1;
    }
    while(x >= 0 && x < N && y >= 0 && y < N) {
      if(grid[x][y] == '1') {
        if(d == 0) d = 1;
        else if(d == 1) d = 0;
        else if(d == 2) d = 3;
        else if(d == 3) d = 2;
      } else if(grid[x][y] == '2') {
        if(d == 0) d = 3;
        else if(d == 1) d = 2;
        else if(d == 2) d = 1;
        else if(d == 3) d = 0;
      }
      int nx = x + dx[d];
      int ny = y + dy[d];
      x = nx;
      y = ny;
    }
    if(x == -1) B[y] = A[port];
    else if(y == N) B[x + N] = A[port];
    else if(x == N) B[3*N-1 - y] = A[port];
    else if(y == -1) B[4*N-1 - x] = A[port];
  }
  return B;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  M = 256;
  A.resize(M);
  REP(i, M) cin >> A[i];
  REP(i, M) A[i]--;
  //そのマスでその方向に向かっている光のID
  vector<int> light_id(N*N*4);
  //そのマスからその方向に向かったときに到達するポート
  vector<int> next_port(N*N*4);
  REP(i, N) REP(j, N) {
    light_id[i*N*4 + j*4 + 0] = A[3*N-1 - j];
    light_id[i*N*4 + j*4 + 1] = A[4*N-1 - i];
    light_id[i*N*4 + j*4 + 2] = A[j];
    light_id[i*N*4 + j*4 + 3] = A[N + i];
    next_port[i*N*4 + j*4 + 0] = j;
    next_port[i*N*4 + j*4 + 1] = N + i;
    next_port[i*N*4 + j*4 + 2] = 3*N-1 - j;
    next_port[i*N*4 + j*4 + 3] = 4*N-1 - i;
  }
  vector<string> grid(N, string(N, '0'));
  vector<int> B = get_B(light_id, grid);

  int best_score = calc_score(B);
  double time = 0;
  int loop = 0;
  double start_temp = 1.0, end_temp = 0.0;
  double temp = start_temp;
  auto start_time = std::chrono::system_clock::now();
  int best_score_2 = best_score;
  vector<string> best_grid = grid;
  int TIME_LIMIT = 1900;
  while(true) {
    if(loop%1000 == 0) {
      auto now_time = std::chrono::system_clock::now();
      time = std::chrono::duration_cast<std::chrono::milliseconds>(now_time-start_time).count();
      temp = start_temp + (end_temp - start_temp) * time / TIME_LIMIT;
      cerr << "LOOP:" << loop << " SCORE:" << best_score << " TIME:" << time << endl;
      if(time > TIME_LIMIT) break;
    }
    loop++;

    int i = mt() % N;
    int j = mt() % N;
    char old_val = grid[i][j];
    char new_val = '0' + (mt() % 3);
    if(mt() % 4 != 0) new_val = '0';
    if(new_val == old_val) continue;
    grid[i][j] = new_val;
    vector<int> new_B = get_B(light_id, grid);
    int new_score = calc_score(new_B);
    double prob = exp(((double)new_score-best_score)/pow(10, temp));
    if(new_score > best_score || prob > (mt()%INF)/(double)INF) {
      best_score = new_score;
      B = new_B;
      if(new_score > best_score_2) {
        best_score_2 = new_score;
        best_grid = grid;
      }
    } else {
      grid[i][j] = old_val;
    }
  }

  REP(i, N) cout << best_grid[i] << "\n";
  cerr << best_score_2 << "\n";
  
}
0