結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-26 15:21:35 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,863 ms / 2,000 ms |
| + 33µs | |
| コード長 | 5,436 bytes |
| 記録 | |
| コンパイル時間 | 2,007 ms |
| コンパイル使用メモリ | 230,236 KB |
| 実行使用メモリ | 9,924 KB |
| スコア | 1,061,817 |
| 最終ジャッジ日時 | 2026-11-26 15:23:18 |
| 合計ジャッジ時間 | 103,122 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge5_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
#include <iostream>
#include <random>
#include <chrono>
using namespace std;
constexpr int N = 64;
constexpr int M = 4 * N;
int A[N];
constexpr int DIR[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; // 上、右、下、左
constexpr int CHDIR[3][4] = {{0, 1, 2, 3}, {1, 0, 3, 2}, {3, 2, 1, 0}}; // board状態がiだったときにj方向で入ったときにどの向きに変わるか
class TimeKeeper {
private:
chrono::high_resolution_clock::time_point start_time;
double time_limit_ms;
public:
TimeKeeper(double limit_ms) : time_limit_ms(limit_ms) {
start_time = chrono::high_resolution_clock::now();
}
double get_elapsed_ms() {
auto now = chrono::high_resolution_clock::now();
return chrono::duration_cast<chrono::milliseconds>(now - start_time).count();
}
bool is_time_up() {
return get_elapsed_ms() >= time_limit_ms;
}
};
void input() {
int _; cin >> _;
for (int i = 0; i < M; i++) cin >> A[i];
}
struct State {
int score;
int state[4*N];
int board[N][N];
State() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = 0;
}
}
update_state();
calc_score();
}
void update_state() {
for (int i = 0; i < N; i++) {
int dir = 2;
int now_x = i;
int now_y = -1;
while (true) {
now_x += DIR[dir][0];
now_y += DIR[dir][1];
if (solution_state(now_x, now_y, i)) break;
dir = CHDIR[board[now_y][now_x]][dir];
}
}
for (int i = 0; i < N; i++) {
int dir = 3;
int now_x = N;
int now_y = i;
while (true) {
now_x += DIR[dir][0];
now_y += DIR[dir][1];
if (solution_state(now_x, now_y, N+i)) break;
dir = CHDIR[board[now_y][now_x]][dir];
}
}
for (int i = 0; i < N; i++) {
int dir = 0;
int now_x = N-i-1;
int now_y = -1;
while (true) {
now_x += DIR[dir][0];
now_y += DIR[dir][1];
if (solution_state(now_x, now_y, 2*N+i)) break;
dir = CHDIR[board[now_y][now_x]][dir];
}
}
for (int i = 0; i < N; i++) {
int dir = 1;
int now_x = -1;
int now_y = N-i-1;
while (true) {
now_x += DIR[dir][0];
now_y += DIR[dir][1];
if (solution_state(now_x, now_y, 3*N+i)) break;
dir = CHDIR[board[now_y][now_x]][dir];
}
}
}
bool solution_state(int now_x, int now_y, int idx) {
if (now_x < 0) {
state[3*N + N-now_y-1] = A[idx];
return true;
}
if (now_x >= N) {
state[N + now_y] = A[idx];
return true;
}
if (now_y < 0) {
state[now_x] = A[idx];
return true;
}
if (now_y >= N) {
state[2*N + N-now_x-1] = A[idx];
return true;
}
return false;
}
void apply(int x, int y, int num) {
board[y][x] = num;
update_state();
calc_score();
}
void calc_score() {
int s = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < i; j++) {
if (state[j] > state[i]) s++;
}
}
score = s;
}
void print() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << board[i][j];
}
cout << endl;
}
}
};
void climbing() {
int time_limit = 1900;
TimeKeeper timer(time_limit);
std::random_device rd;
std::mt19937 gen(rd());
const double start_temperature = 10;
const double end_temperature = 1;
double current_temperature = start_temperature;
std::uniform_real_distribution<double> zero_one_dist(0.0, 1.0);
std::uniform_int_distribution<int> dist(0, N-1); // 乱数生成
std::uniform_int_distribution<int> board_dist(0, 2); // 乱数生成
int iteration = 0;
State now_state = State();
State best_state = State();
while (!timer.is_time_up()) {
// 近傍を変更
int x = dist(gen);
int y = dist(gen);
int prev_board_num = now_state.board[y][x];
int board_num = board_dist(gen);
int old_score = now_state.score;
now_state.apply(x, y, board_num);
int new_score = now_state.score;
int delta = new_score - old_score; // 悪化すると delta > 0 になるように定義
// cerr << x << " " << y << " " << board_num << endl;
// cerr << delta << endl;
if (delta <= 0 || exp(-delta / current_temperature) > zero_one_dist(gen)) {
} else {
now_state.apply(x, y, prev_board_num);
}
double progress = timer.get_elapsed_ms() / (double) time_limit;
current_temperature = std::pow(start_temperature, 1.0 - progress) * std::pow(end_temperature, progress);
if (now_state.score < best_state.score) {
best_state = now_state;
}
iteration++;
}
cerr << iteration << endl;
best_state.print();
}
int main() {
input();
climbing();
}