結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-22 22:34:56 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,891 ms / 2,000 ms |
| + 867µs | |
| コード長 | 11,238 bytes |
| 記録 | |
| コンパイル時間 | 3,570 ms |
| コンパイル使用メモリ | 283,376 KB |
| 実行使用メモリ | 9,908 KB |
| スコア | 1,460,431 |
| 最終ジャッジ日時 | 2026-11-23 20:53:23 |
| 合計ジャッジ時間 | 104,575 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge5_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
// tester解
// 解説スライドのStep6相当の解法ですが、細かい部分は一致していません。
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <chrono>
#include <cmath>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
// 光の進行方向 (インデックスは dx, dy と対応)
constexpr int RIGHT = 0;
constexpr int DOWN = 1;
constexpr int LEFT = 2;
constexpr int UP = 3;
constexpr int dx[4] = { 0, 1, 0, -1 }; // x: 行
constexpr int dy[4] = { 1, 0, -1, 0 }; // y: 列
// 鏡の種類
constexpr int NO_MIRROR = 0;
constexpr int SLASH = 1; // /
constexpr int BACKSLASH = 2; // バックスラッシュ
// through_lights の値: そのマスをその方向に通る光が存在しない
constexpr int NO_LIGHT = -1;
std::mt19937 mt_for_action(0);
// 時間をDouble型で管理し、経過時間も取り出せるクラス
class TimeKeeperDouble {
private:
std::chrono::high_resolution_clock::time_point start_time_;
double time_threshold_;
double now_time_ = 0;
public:
// 時間制限をミリ秒単位で指定してインスタンスをつくる。
TimeKeeperDouble(const double time_threshold)
: start_time_(std::chrono::high_resolution_clock::now()),
time_threshold_(time_threshold) {
}
// 経過時間をnow_time_に格納する。
void setNowTime() {
auto diff = std::chrono::high_resolution_clock::now() - this->start_time_;
this->now_time_ =
std::chrono::duration_cast<std::chrono::microseconds>(diff).count() *
1e-3; // ms
}
// 経過時間(ms)を取得する。
double getNowTime() const { return this->now_time_; }
// インスタンス生成した時から指定した時間制限を超過したか判定する。
bool isTimeOver() const { return now_time_ >= time_threshold_; }
};
int TIME_THR = 1900; // 時間制限 (ms)
int N, M; // 盤面の大きさ、ポート数 (M = 4N)
vector<int> a; // a[i]: ポートiから入る光の番号
// 座標クラス
class Coord {
public:
int x, y;
Coord() : x(0), y(0) {}
Coord(int _x, int _y) : x(_x), y(_y) {}
void move(int dir) {
x += dx[dir];
y += dy[dir];
}
bool in_board() const {
return 0 <= x && x < N && 0 <= y && y < N;
}
};
// 光の開始状態 (座標と進行方向)
struct StartStatus {
Coord coord;
int dir;
};
// 入力ポート番号から、始めの座標と方向を求める
StartStatus get_start_status(int in_port) {
if (in_port < N) {
return { Coord(0, in_port), DOWN };
}
else if (in_port < 2 * N) {
return { Coord(in_port % N, N - 1), LEFT };
}
else if (in_port < 3 * N) {
return { Coord(N - 1, N - 1 - in_port % N), UP };
}
else {
return { Coord(N - 1 - in_port % N, 0), RIGHT };
}
}
// 盤面の外に出た座標から、出力ポート番号を求める
int get_out_port(const Coord& coord) {
if (coord.x == -1) {
return coord.y;
}
else if (coord.y == N) {
return N + coord.x;
}
else if (coord.x == N) {
return N * 2 + (N - 1 - coord.y);
}
else {
return N * 3 + (N - 1 - coord.x);
}
}
// マスに方向dirで入った光が、鏡mirrorによって進む方向を求める
int reflect_dir(int dir, int mirror) {
if (mirror == SLASH) {
return 3 - dir; // 0<->3, 1<->2
}
if (mirror == BACKSLASH) {
return dir ^ 1; // 0<->1, 2<->3
}
return dir;
}
// 盤面
class Board {
public:
// 鏡の配置
vector<vector<int>> mirror;
// 各出力ポートが受け取る光の番号 (受信番号列)
vector<int> b;
// 転倒数
int inversion;
// through_lights[x][y][k]: (x,y)に方向kで入ってくる光の出力ポート (なければNO_LIGHT)
vector<vector<vector<int>>> through_lights;
Board() {
mirror.resize(N, vector<int>(N, NO_MIRROR));
set_b();
set_inversion();
}
void print() const {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << mirror[i][j];
}
cout << endl;
}
}
// ポートin_portから出た光をシミュレートし、出力ポートを返す
// cellsには、通ったマスと侵入方向が追加される
int simulate_light(int in_port, vector<pair<Coord, int>>& cells) const {
auto start = get_start_status(in_port);
Coord coord = start.coord;
int dir = start.dir;
while (coord.in_board()) {
cells.push_back(make_pair(coord, dir));
dir = reflect_dir(dir, mirror[coord.x][coord.y]);
coord.move(dir);
}
return get_out_port(coord);
}
// in_portから出た光の経路を、現在の鏡の配置で b と through_lights に反映
void apply_light(int in_port) {
vector<pair<Coord, int>> cells;
int out_port = simulate_light(in_port, cells);
b[out_port] = a[in_port];
for (const auto& cell : cells) {
through_lights[cell.first.x][cell.first.y][cell.second] = out_port;
}
}
// b(受信番号列)を計算
void set_b() {
// 初期化
b.resize(M, -1);
through_lights.resize(N, vector<vector<int>>(N, vector<int>(4)));
for (int in_port = 0; in_port < M; in_port++) {
apply_light(in_port);
}
}
// 転倒数を計算
void set_inversion() {
inversion = 0;
for (int i = 0; i < M; i++) {
for (int j = i + 1; j < M; j++) {
if (b[i] > b[j]) {
inversion++;
}
}
}
}
// b(出力された番号)の2点をSwapしたときの転倒数差分を計算
int calc_b_swap_inversion_diff(int id0, int id1) const {
if (id0 > id1) swap(id0, id1);
int diff = (b[id0] < b[id1]) ? 1 : -1;
for (int i = id0 + 1; i < id1; i++) {
diff += (b[id0] < b[i]) ? 1 : -1;
diff += (b[i] < b[id1]) ? 1 : -1;
}
return diff;
}
// (x,y)の鏡をnew_mirrorに変えたときの転倒数の増加量を計算
// (計算中にbを一時的に書き換えるが、戻してから返す)
int calc_change_score_diff(int x, int y, int new_mirror) {
int old_mirror = mirror[x][y];
if (old_mirror == new_mirror) {
return 0;
}
// 鏡の変更で入れ替わる出力ポートの組 (through_lightsの方向番号のペア)
// 添字は old_mirror + new_mirror - 1
// {0,1}: / の有無 -> (0,3), (1,2)
// {0,2}: \ の有無 -> (0,1), (2,3)
// {1,2}: / と \ -> (0,2), (1,3)
static const int swap_dirs[3][2][2] = {
{{0, 3}, {1, 2}},
{{0, 1}, {2, 3}},
{{0, 2}, {1, 3}},
};
const auto& lights = through_lights[x][y];
vector<pair<int, int>> swap_out_ports;
for (const auto& d : swap_dirs[old_mirror + new_mirror - 1]) {
swap_out_ports.push_back(make_pair(lights[d[0]], lights[d[1]]));
}
// swapされるペアごとに転倒数がどう変わるかを計算
int diff = 0;
for (const auto& p : swap_out_ports) {
if (p.first == p.second) {
continue;
}
if (p.first == NO_LIGHT || p.second == NO_LIGHT) {
continue;
}
diff += calc_b_swap_inversion_diff(p.first, p.second);
swap(b[p.first], b[p.second]);
}
// bを更新した分を戻す
for (const auto& p : swap_out_ports) {
if (p.first == NO_LIGHT || p.second == NO_LIGHT) {
continue;
}
swap(b[p.first], b[p.second]);
}
return diff;
}
// 指定した座標に鏡があり、その鏡が必要ないかを判定
// (鏡の片側に光がない状態 = 除去しても同じ)
bool is_unneed_mirror(int x, int y) const {
if (mirror[x][y] == NO_MIRROR) {
return false;
}
for (int out_port : through_lights[x][y]) {
if (out_port == NO_LIGHT) {
return true;
}
}
return false;
}
// (x,y)の鏡をnew_mirrorに変更
void change(int x, int y, int new_mirror, int diff) {
auto in_ports = through_lights[x][y];
inversion += diff;
// through_lights更新のため、古い鏡の配置でシミュレーションして消去
for (int in_port : in_ports) {
if (in_port == NO_LIGHT) {
continue;
}
vector<pair<Coord, int>> cells;
simulate_light(in_port, cells);
for (const auto& cell : cells) {
through_lights[cell.first.x][cell.first.y][cell.second] = NO_LIGHT;
}
}
// 鏡の更新
mirror[x][y] = new_mirror;
// 更新後の鏡で再度シミュレーション
for (int in_port : in_ports) {
if (in_port == NO_LIGHT) {
continue;
}
apply_light(in_port);
}
}
};
Board solve() {
auto board = Board();
auto best_board = board;
auto time_keeper = TimeKeeperDouble(TIME_THR);
int cnt = 0;
int accept = 0;
double start_temp = 2;
double end_temp = 0;
while (true) {
time_keeper.setNowTime();
if (time_keeper.isTimeOver()) {
break;
}
cnt++;
// 近傍生成
int i = mt_for_action() % N;
int j = mt_for_action() % N;
bool ok = false;
int new_mirror;
int diff;
if (board.is_unneed_mirror(i, j)) {
ok = true;
new_mirror = NO_MIRROR;
diff = 0;
}
else {
new_mirror = mt_for_action() % 3;
if (new_mirror == board.mirror[i][j]) {
continue;
}
// スコア差分を計算
diff = board.calc_change_score_diff(i, j, new_mirror);
// 遷移判定
double temp =
start_temp +
(end_temp - start_temp) * (time_keeper.getNowTime() / TIME_THR);
int big_M = 100000000;
double r = double(mt_for_action() % big_M) / big_M;
ok = exp(-1 * diff / temp) > r;
}
if (ok) {
board.change(i, j, new_mirror, diff);
accept++;
}
if (best_board.inversion > board.inversion) {
best_board = board;
}
}
cerr << "cnt: " << cnt << endl;
cerr << "accept: " << accept << endl;
cerr << "inversion: " << best_board.inversion << endl;
cerr << "score: " << M * (M - 1) / 2 - best_board.inversion << endl;
return best_board;
}
int main() {
cin >> N;
M = N * 4;
a.resize(M);
for (int i = 0; i < M; i++) {
cin >> a[i];
}
auto ans = solve();
ans.print();
}