結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-26 20:52:04 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,852 ms / 2,000 ms |
| + 374µs | |
| コード長 | 9,211 bytes |
| 記録 | |
| コンパイル時間 | 4,763 ms |
| コンパイル使用メモリ | 358,896 KB |
| 実行使用メモリ | 9,828 KB |
| スコア | 1,426,673 |
| 最終ジャッジ日時 | 2026-11-26 20:53:49 |
| 合計ジャッジ時間 | 103,971 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge6_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, mai, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // fiied,setprecision
#include <limits.h> // INT_MAi
#include <math.h> // M_PI
#include <random> // random_device, mt19937, uniform_int_distribution
#include <regex> // 正規表現
#include <time.h> // time
#include <fstream> // ifstream, ofstream
#include <array> // array
#include <bit> // bit
#include <chrono> // chrono
#include <span> // span
#include <cmath> // sqrt, sin, cos, tan, asin, acos, atan, atan2
#include <complex> // 複素数
// #include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
using ll = long long;
using ull = unsigned long long;
using ld = long double; // 80bit 18桁
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const string DIR = "URDL";
const int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
// INFに3以上を掛ける、またはint(INF)に1以上を足すとオーバーフローする
// 4611686016279904255 cf: LLONG_MAi=9223372036854775807
// const ll INF = (1ll << 62) - (1ll << 31) - 1ll;
// using P=pair<ll,ll>;
const int INF = 1001001001;
//-----------------------------------------------------
// 乱数生成器
std::mt19937 mt{42};
std::uniform_real_distribution<double> zero_one_dist(0.0, 1.0);
int RandInt(int l, int r) {
return mt() % (r - l + 1) + l;
}
const int N = 64;
const int M = 256;
vector<int> a(M),b(M);
int calc_score(const vector<string>& out)
{
rep(i, M) {
int x, y, dir;
if (i < N) {
x = 0;
y = i;
dir = 2; // 下
} else if (i < 2 * N) {
x = i - N;
y = N - 1;
dir = 3; // 左
} else if (i < 3 * N) {
x = N - 1;
y = 3 * N - 1 - i;
dir = 0; // 上
} else {
x = 4 * N - 1 - i;
y = 0;
dir = 1; // 右
}
// 方向の順番は U, R, D, L
constexpr int slash[4] = {1, 0, 3, 2};
constexpr int backslash[4] = {3, 2, 1, 0};
while (0 <= x && x < N && 0 <= y && y < N) {
if (out[x][y] == '1') {
dir = slash[dir];
} else if (out[x][y] == '2') {
dir = backslash[dir];
}
x += dx[dir];
y += dy[dir];
}
int exit;
if (x < 0) {
exit = y;
} else if (x >= N) {
exit = 3 * N - 1 - y;
} else if (y < 0) {
exit = 4 * N - 1 - x;
} else {
exit = N + x;
}
b[exit] = a[i];
}
int inversions = 0;
rep(i, M) {
for (int j = i + 1; j < M; ++j) {
if (b[i] > b[j]) ++inversions;
}
}
return M * (M - 1) / 2 - inversions;
}
// (x, y) の side 側から、対象マスの外へ光をたどる。
// 外周に出ればポート番号、対象マスに戻れば -1 を返す。
int trace_port_from_side(const vector<string>& out, int x, int y, int side)
{
constexpr int next_dir[3][4] = {
{0, 1, 2, 3}, // '0'
{1, 0, 3, 2}, // '1' /
{3, 2, 1, 0}, // '2' backslash
};
int dir = side;
int row = x + dx[dir];
int col = y + dy[dir];
while (0 <= row && row < N && 0 <= col && col < N) {
if (row == x && col == y) return -1;
dir = next_dir[out[row][col] - '0'][dir];
row += dx[dir];
col += dy[dir];
}
if (row < 0) return col;
if (row >= N) return 3 * N - 1 - col;
if (col < 0) return 4 * N - 1 - row;
return N + row;
}
// b を変更後の状態に更新し、スコア差(新 - 旧)を返す。
// 棄却時に戻せるよう、変更したポートと元の値を undo に記録する。
int apply_score_delta(const vector<string>& out, int x, int y, int type,
array<pair<int, int>, 4>& undo, int& undo_count)
{
// [種類][光が入る辺] → 光が出る辺。辺の順番は U, R, D, L。
constexpr int paired_side[3][4] = {
{2, 3, 0, 1}, // '0' U-D, R-L
{3, 2, 1, 0}, // '1' U-L, R-D
{1, 0, 3, 2}, // '2' U-R, D-L
};
undo_count = 0;
int ports[4];
for (int side = 0; side < 4; ++side) {
ports[side] = trace_port_from_side(out, x, y, side);
if (ports[side] == -1) return 0;
}
int inversion_delta = 0;
for (int side = 0; side < 4; ++side) {
const int pos = ports[side];
const int new_value = a[ports[paired_side[type][side]]];
const int old_value = b[pos];
if (new_value == old_value) continue;
for (int j = 0; j < pos; ++j) {
inversion_delta += (b[j] > new_value) - (b[j] > old_value);
}
for (int j = pos + 1; j < M; ++j) {
inversion_delta += (new_value > b[j]) - (old_value > b[j]);
}
undo[undo_count++] = {pos, old_value};
b[pos] = new_value;
}
return -inversion_delta;
}
vector<string> solve()
{
//cerr << "Starting solve" << endl;
vector<string> out(N),best_out(N);
rep(i, N)
{
out[i].resize(N);
best_out[i].resize(N);
rep(j, N)
{
out[i][j] = '0';
best_out[i][j] = '0';
}
}
vector<bool>tate(N, false), yoko(N, false);
vector<int>order_tate(N), order_yoko(N);
rep(i,N){
order_tate[i] = i;
order_yoko[i] = i;
}
shuffle(order_tate.begin(), order_tate.end(), mt19937(random_device()()));
shuffle(order_yoko.begin(), order_yoko.end(), mt19937(random_device()()));
rep(i,N){
out[order_tate[i]][order_yoko[i]] = '1';
best_out[order_tate[i]][order_yoko[i]] = '1';
}
//焼きなまし法にする
// 開始温度と終了温度
const double start_temperature = 3;
const double end_temperature = 0.1;
// 現在の温度
double current_temperature = start_temperature;
// 試行回数
int iteration = 0;
int TIME_LIMIT = 1.85*CLOCKS_PER_SEC; // 適当な時間制限を設定
int ti=clock();
int current_score = calc_score(out); // 初期状態の b も作る
int best = current_score;
cerr << "Initial best score: " << best << endl;
while(clock() - ti < TIME_LIMIT){
int i = RandInt(0, N-1);
int j = RandInt(0, N-1);
int old=out[i][j]-'0';
int type=RandInt(0, 2);
while(type==old) type=RandInt(0, 2);
//cerr << "Trying type " << type << " with positions (" << i << "," << j << ") and (" << i2 << "," << j2 << ")" << endl;
//cerr << "Current score: " << score << endl;
array<pair<int, int>, 4> undo;
int undo_count;
const int score = current_score
+ apply_score_delta(out, i, j, type, undo, undo_count);
const int delta = score - current_score;
//if (delta >= 0 ||
// zero_one_dist(mt) < exp(delta / current_temperature)) {
if (delta >= 0){
out[i][j] = '0' + type;
current_score = score;
if (score > best) {
cerr << "New best score: " << score << '\n';
best = score;
best_out = out;
}
} else {
for (int k = 0; k < undo_count; ++k) {
const auto [pos, old_value] = undo[k];
b[pos] = old_value;
}
}
//cerr<<__LINE__<<"-----"<<endl;
iteration++;
if(iteration % 10000 == 0) cerr << "Iteration: " << iteration << " Current temperature: " << current_temperature << endl;
// 温度を更新
current_temperature = start_temperature + (end_temperature - start_temperature) * (double)(clock() - ti) / TIME_LIMIT;
}
//cerr<<__LINE__<<"-----"<<out.size()<<endl;
//for(int i = 0; i < N; i++){
// cerr << best_out[i] << endl;
//}
cerr<<"best score: " << best << endl;
return best_out;
}
int main()
{
int n;
cin >> n;
rep(i, M) cin >> a[i];
vector<string> out = solve();
//cerr<<__LINE__<<"-----"<<endl;
rep(i, N) cout << out[i] << endl;
cerr << "Best score: " << calc_score(out) << endl;
//rep(i,M)cerr<<"a["<<i<<"]="<<a[i]<<" b["<<i<<"]="<<b[i]<<endl;
return 0;
}