結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー Shun_PI
提出日時 2026-11-23 22:59:25
言語 C++14
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,891 ms / 2,000 ms
+ 204µs
コード長 8,222 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,391 ms
コンパイル使用メモリ 232,044 KB
実行使用メモリ 9,904 KB
スコア 1,470,663
最終ジャッジ日時 2026-11-23 23:11:18
合計ジャッジ時間 103,198 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_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;
}

int calc_score_easy(vector<int> &B) {
  int score = 0;
  REP(i, M) score += abs(i - B[i]);
  return M * (M-1) / 2 - score;
}

vector<int> get_B(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(grid);

  int best_score = calc_score_easy(B);
  double time = 0;
  int loop = 0;
  double start_temp = 0.8, end_temp = -0.2;
  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;
    //スコアの差分計算
    int new_score = best_score;
    REP(d, 4) if(light_id[i*N*4 + j*4 + d] != -1) new_score += abs(light_id[i*N*4 + j*4 + d] - next_port[i*N*4 + j*4 + d]);
    grid[i][j] = new_val;
    //そのマスだけ光の方向を更新する
    if((old_val == '0' && new_val == '1') || (old_val == '1' && new_val == '0')) {
      swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 1]);
      swap(light_id[i*N*4 + j*4 + 2], light_id[i*N*4 + j*4 + 3]);
    } else if(old_val == '0' && new_val == '2' || old_val == '2' && new_val == '0') {
      swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 3]);
      swap(light_id[i*N*4 + j*4 + 1], light_id[i*N*4 + j*4 + 2]);
    } else if(old_val == '1' && new_val == '2' || old_val == '2' && new_val == '1') {
      swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 2]);
      swap(light_id[i*N*4 + j*4 + 1], light_id[i*N*4 + j*4 + 3]);
    }
    REP(d, 4) if(light_id[i*N*4 + j*4 + d] != -1) new_score -= abs(light_id[i*N*4 + j*4 + d] - next_port[i*N*4 + j*4 + d]);
    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;
      //light_idとnext_portを再構築する必要がある
      //i,jから4方向を探索する
      REP(d, 4) {
        int ci = i, cj = j, cd = d;
        int light = light_id[ci*N*4 + cj*4 + cd];
        int mirror_d = (d + 2) % 4;
        if(grid[ci][cj] == '1') {
          if(cd == 0) mirror_d = 3;
          else if(cd == 1) mirror_d = 2;
          else if(cd == 2) mirror_d = 1;
          else if(cd == 3) mirror_d = 0;
        } else if(grid[ci][cj] == '2') {
          if(cd == 0) mirror_d = 1;
          else if(cd == 1) mirror_d = 0;
          else if(cd == 2) mirror_d = 3;
          else if(cd == 3) mirror_d = 2;
        }
        int port = next_port[ci*N*4 + cj*4 + mirror_d];
        int patience = 256;
        int last_d = cd;
        while(ci >= 0 && ci < N && cj >= 0 && cj < N) {
          ci += dx[cd];
          cj += dy[cd];
          last_d = cd;
          patience--;
          if(patience == 0) {
            light = -1;
            port = -1;
          } else if(patience == -256) {
            break;
          }
          if(ci < 0 || ci >= N || cj < 0 || cj >= N) break;
          next_port[ci*N*4 + cj*4 + (last_d + 2) % 4] = port;
          if(grid[ci][cj] == '1') {
            if(cd == 0) cd = 1;
            else if(cd == 1) cd = 0;
            else if(cd == 2) cd = 3;
            else if(cd == 3) cd = 2;
          } else if(grid[ci][cj] == '2') {
            if(cd == 0) cd = 3;
            else if(cd == 1) cd = 2;
            else if(cd == 2) cd = 1;
            else if(cd == 3) cd = 0;
          }
          light_id[ci*N*4 + cj*4 + cd] = light;
        }
      }
      if(new_score > best_score_2) {
        best_score_2 = new_score;
        best_grid = grid;
      }
    } else {
      grid[i][j] = old_val;
      if((old_val == '0' && new_val == '1') || (old_val == '1' && new_val == '0')) {
        swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 1]);
        swap(light_id[i*N*4 + j*4 + 2], light_id[i*N*4 + j*4 + 3]);
      } else if((old_val == '0' && new_val == '2') || (old_val == '2' && new_val == '0')) {
        swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 3]);
        swap(light_id[i*N*4 + j*4 + 1], light_id[i*N*4 + j*4 + 2]);
      } else if((old_val == '1' && new_val == '2') || (old_val == '2' && new_val == '1')) {
        swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 2]);
        swap(light_id[i*N*4 + j*4 + 1], light_id[i*N*4 + j*4 + 3]);
      }
    }
  }

  REP(i, N) cout << best_grid[i] << "\n";
  vector<int> final_B = get_B(best_grid);
  cerr << calc_score(final_B) << "\n";
  
}
0