結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
prussian_coder
|
| 提出日時 | 2026-11-21 18:25:32 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,991 ms / 2,000 ms |
| + 405µs | |
| コード長 | 6,603 bytes |
| 記録 | |
| コンパイル時間 | 2,887 ms |
| コンパイル使用メモリ | 367,476 KB |
| 実行使用メモリ | 9,904 KB |
| スコア | 1,495,281 |
| 最終ジャッジ日時 | 2026-11-23 20:43:16 |
| 合計ジャッジ時間 | 108,102 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge6_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
// main.py の C++ 移植: 鏡の配置を焼きなまし法で改善する解法。
#include <bits/stdc++.h>
using namespace std;
// 方向は下・左・上・右の順。盤面外周の入口番号もこの順に対応する。
const int DR[4] = {1, 0, -1, 0};
const int DC[4] = {0, -1, 0, 1};
const char OUTPUT_CODES[4] = {'0', '1', '?', '2'}; // 0,1,3 のみ使用
const double TIME_LIMIT = 2.0;
const double START_TEMPERATURE = 100.0;
const double END_TEMPERATURE = 1.0;
mt19937 rng(random_device{}());
uniform_real_distribution<double> uniform01(0.0, 1.0);
auto start_clock = chrono::steady_clock::now();
double elapsed() {
return chrono::duration<double>(chrono::steady_clock::now() - start_clock).count();
}
struct Solver {
int n;
int width; // 外周の入口・出口を含む盤面幅
vector<int> ball_values;
vector<int> board;
vector<int> entrance_cells;
vector<int> entrance_at; // cell -> entrance index, なければ -1
// incoming[cell][direction]: その方向からセルに入るボールの番号
vector<array<int, 4>> incoming;
Solver(int n_, const vector<int>& ball_values_)
: n(n_), width(n_ + 2), ball_values(ball_values_) {
int cells = width * width;
board.assign(cells, 0);
entrance_at.assign(cells, -1);
incoming.assign(cells, {-1, -1, -1, -1});
entrance_cells = make_entrance_cells();
for (int i = 0; i < (int)entrance_cells.size(); i++) {
entrance_at[entrance_cells[i]] = i;
}
for (int ball_id = 0; ball_id < 4 * n; ball_id++) {
trace_and_record(ball_id);
}
}
int cell(int row, int col) const { return row * width + col; }
int move(int c, int direction) const {
int row = c / width;
int col = c % width;
return cell(row + DR[direction], col + DC[direction]);
}
vector<int> make_entrance_cells() const {
// 入口番号 0..4N-1 に対応する外周セルを作る。
vector<int> cells(4 * n, -1);
for (int entrance = 0; entrance < 4 * n; entrance++) {
int offset = entrance % n;
if (entrance < n) { // 上辺: 左から右
cells[entrance] = cell(0, offset + 1);
} else if (entrance < 2 * n) { // 右辺: 上から下
cells[entrance] = cell(offset + 1, width - 1);
} else if (entrance < 3 * n) { // 下辺: 右から左
cells[entrance] = cell(width - 1, width - 2 - offset);
} else { // 左辺: 下から上
cells[entrance] = cell(width - 2 - offset, 0);
}
}
return cells;
}
static int reflect(int direction, int mirror) { return direction ^ mirror; }
void erase_balls(int ball_id) {
int c = entrance_cells[ball_id];
int direction = ball_id / n;
while (true) {
c = move(c, direction);
if (entrance_at[c] != -1) return;
incoming[c][direction] = -1;
direction = reflect(direction, board[c]);
}
}
void trace_and_record(int ball_id) {
// ボールの経路をたどり、通過セルの入射方向を記録する。
int c = entrance_cells[ball_id];
int direction = ball_id / n;
while (true) {
c = move(c, direction);
if (entrance_at[c] != -1) return;
incoming[c][direction] = ball_id;
direction = reflect(direction, board[c]);
}
}
int cost_if_mirror(int c, int mirror) const {
// セルに mirror を置いたときの局所的な評価値を返す。
const auto& inc = incoming[c];
bool has_empty = false;
for (int d = 0; d < 4; d++) {
if (inc[d] == -1) {
has_empty = true;
break;
}
}
if (has_empty) {
return mirror == 0 ? 1000 : -1000;
}
int cost = 0;
for (int i = 0; i < 4; i++) {
int value = ball_values[inc[i]];
int paired_id = inc[i ^ mirror ^ 2];
cost += value * paired_id;
}
return cost;
}
double temperature(double elapsed_sec) const {
double progress = elapsed_sec / TIME_LIMIT;
return START_TEMPERATURE * (1.0 - progress) + END_TEMPERATURE * progress;
}
bool try_change_mirror(int c) {
int old_mirror = board[c];
// Python: random.randint(0, 4) の後、偶数なら 0 に落とす → 0, 1, 3
int new_mirror = int(rng() % 5);
if (new_mirror % 2 == 0) new_mirror = 0;
if (old_mirror == new_mirror) return false;
int old_cost = cost_if_mirror(c, old_mirror);
int new_cost = cost_if_mirror(c, new_mirror);
int delta = new_cost - old_cost;
double temp = temperature(elapsed());
if (delta > temp * log(uniform01(rng))) {
vector<int> change_balls;
change_balls.reserve(4);
for (int ball : incoming[c]) {
if (ball != -1) change_balls.push_back(ball);
}
for (int ball_id : change_balls) erase_balls(ball_id);
board[c] = new_mirror;
for (int ball_id : change_balls) trace_and_record(ball_id);
return true;
}
return false;
}
pair<long long, long long> optimize() {
// 時間制限まで鏡の変更を試し、試行数と採用数を返す。
long long attempts = 0;
long long accepted = 0;
while (elapsed() < TIME_LIMIT) {
attempts++;
int row = int(rng() % n);
int col = int(rng() % n);
int c = cell(row + 1, col + 1);
if (try_change_mirror(c)) accepted++;
}
return {attempts, accepted};
}
void write_answer() const {
// 盤面内の鏡だけを標準出力に書き出す。
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
int c = cell(row + 1, col + 1);
cout << OUTPUT_CODES[board[c]];
}
cout << '\n';
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
start_clock = chrono::steady_clock::now();
int n;
cin >> n;
vector<int> ball_values(4 * n);
for (int& v : ball_values) cin >> v;
Solver solver(n, ball_values);
auto [attempts, accepted] = solver.optimize();
cerr << attempts << ' ' << accepted << '\n';
solver.write_answer();
return 0;
}
prussian_coder