結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
fuppy_kyopro
|
| 提出日時 | 2026-11-26 00:20:47 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,910 ms / 2,000 ms |
| + 568µs | |
| コード長 | 17,714 bytes |
| 記録 | |
| コンパイル時間 | 4,779 ms |
| コンパイル使用メモリ | 393,336 KB |
| 実行使用メモリ | 7,856 KB |
| スコア | 1,438,533 |
| 最終ジャッジ日時 | 2026-11-26 00:22:36 |
| 合計ジャッジ時間 | 107,879 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
コンパイルメッセージ
In member function 'bool AnnealingState::update_best()',
inlined from 'void AnnealingState::init()' at main.cpp:345:20,
inlined from 'AnnealingState::AnnealingState(double)' at main.cpp:336:13,
inlined from 'int main(int, char**)' at main.cpp:592:29:
main.cpp:581:24: warning: 'anneal.AnnealingState::state' may be used uninitialized [-Wmaybe-uninitialized]
581 | best_state = state;
| ~~~~~~~~~~~^~~~~~~
main.cpp: In function 'int main(int, char**)':
main.cpp:592:20: note: 'anneal' declared here
592 | AnnealingState anneal(TL);
| ^~~~~~
In member function 'bool AnnealingState::should_accept(Cost) const',
inlined from 'bool AnnealingState::should_accept(Cost) const' at main.cpp:568:10,
inlined from 'void AnnealingState::step()' at main.cpp:548:26,
inlined from 'void AnnealingState::solve()' at main.cpp:473:17,
inlined from 'int main(int, char**)' at main.cpp:593:17:
main.cpp:574:34: warning: 'anneal.AnnealingState::temperature' may be used uninitialized [-Wmaybe-uninitialized]
574 | return randdouble() < exp(-score_diff / temperature);
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main(int, char**)':
main.cpp:592:20: note: 'anneal.AnnealingState::temperature' was declared here
592 | AnnealingState anneal(TL);
| ^~~~~~
ソースコード
// ---------------- ここからテンプレ、修正しないこと -----------------
//*
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
//*/
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
// #define _GLIBCXX_DEBUG
#ifdef LOCAL
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v) \
cerr << #v << ":"; \
for (int iiiiiiii = 0; iiiiiiii < v.size(); iiiiiiii++) \
cerr << " " << v[iiiiiiii]; \
cerr << endl;
#define DEBUG_MAT(v) \
cerr << #v << endl; \
for (int iv = 0; iv < v.size(); iv++) { \
for (int jv = 0; jv < v[iv].size(); jv++) { \
cerr << v[iv][jv] << " "; \
} \
cerr << endl; \
}
#else
#define DEBUG(x) ;
#define DEBUG_VEC(v) ;
#define DEBUG_MAT(v) ;
#endif
typedef long long ll;
// #define int ll
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
return pair<S, T>(s.first - t.first, s.second - t.second);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i > 0; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(), c.end()
void YES(bool t = true) { cout << (t ? "YES" : "NO") << endl; }
void Yes(bool t = true) { cout << (t ? "Yes" : "No") << endl; }
void yes(bool t = true) { cout << (t ? "yes" : "no") << endl; }
void NO(bool t = true) { cout << (t ? "NO" : "YES") << endl; }
void No(bool t = true) { cout << (t ? "No" : "Yes") << endl; }
void no(bool t = true) { cout << (t ? "no" : "yes") << endl; }
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U> T ceil_div(T a, U b) { return (a + b - 1) / b; }
template <class T> T safe_mul(T a, T b) {
if (a == 0 || b == 0)
return 0;
if (numeric_limits<T>::max() / a < b)
return numeric_limits<T>::max();
return a * b;
}
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
int popcount(ll t) { return __builtin_popcountll(t); }
vector<int> gen_perm(int n) {
vector<int> ret(n);
iota(all(ret), 0);
return ret;
}
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {0, 1, 0, -1}, dy = {-1, 0, 1, 0};
vi dx2 = {1, 1, 0, -1, -1, -1, 0, 1}, dy2 = {0, 1, 1, 1, 0, -1, -1, -1};
struct Setup_io {
Setup_io() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(25);
cerr << fixed << setprecision(25);
}
} setup_io;
// constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
// #define mp make_pair
auto start = chrono::system_clock::now();
constexpr double TL = 1.95;
double calc_seconds() {
return static_cast<double>(chrono::duration_cast<chrono::microseconds>(
chrono::system_clock::now() - start)
.count() /
1000000.0);
}
#ifdef LOCAL
struct TimerRegistry {
struct Entry {
double total = 0.0;
int count = 0;
};
unordered_map<string, Entry> data;
static TimerRegistry &instance() {
static TimerRegistry reg;
return reg;
}
void add(const string &name, double elapsed) {
data[name].total += elapsed;
data[name].count++;
}
void print() const {
vector<pair<string, Entry>> v(data.begin(), data.end());
sort(v.begin(), v.end(),
[](auto &a, auto &b) { return a.second.total > b.second.total; });
cerr << "=== Timer Report ===" << endl;
for (auto &[name, e] : v) {
cerr << name << ": total=" << e.total << "s"
<< " count=" << e.count << " avg=" << e.total / e.count
<< "s" << endl;
}
}
~TimerRegistry() { print(); }
};
struct ScopedTimer {
string name;
chrono::time_point<chrono::system_clock> t0;
ScopedTimer(const string &n) : name(n), t0(chrono::system_clock::now()) {}
~ScopedTimer() {
double elapsed =
chrono::duration<double>(chrono::system_clock::now() - t0).count();
TimerRegistry::instance().add(name, elapsed);
}
};
#define TIMER(name) ScopedTimer _st_##name(#name)
#define PRINT_TIMERS() TimerRegistry::instance().print()
#else
#define TIMER(name)
#define PRINT_TIMERS()
#endif
class HRandom {
public:
HRandom() {
random_mw = 1985;
random_mz = 2012;
}
int intRandom() {
random_mz = 36969 * (random_mz & 65535) + (random_mz >> 16);
random_mw = 18000 * (random_mw & 65535) + (random_mw >> 16);
unsigned result = (random_mz << 16) + random_mw;
return result >> 1;
}
unsigned int uintRandom_65535() {
// random_mz = 36969 * (random_mz & 65535) + (random_mz >> 16);
// random_mw = 18000 * (random_mw & 65535) + (random_mw >> 16);
// return random_mw & 65535;
const unsigned tmp = random_mw & 65535;
random_mw = 18000 * tmp + (random_mw >> 16);
return tmp;
}
unsigned int uintRandom() {
random_mz = 36969 * (random_mz & 65535) + (random_mz >> 16);
random_mw = 18000 * (random_mw & 65535) + (random_mw >> 16);
unsigned result = (random_mz << 16) + random_mw;
return result;
}
double dblRandom() {
random_mz = 36969 * (random_mz & 65535) + (random_mz >> 16);
random_mw = 18000 * (random_mw & 65535) + (random_mw >> 16);
unsigned result = (random_mz << 16) + random_mw;
return (result + 1.0) * 2.328306435454494e-10;
}
private:
unsigned random_mw;
unsigned random_mz;
};
HRandom hrandom;
static uint32_t randxor() {
static uint32_t x = 123456789;
static uint32_t y = 362436069;
static uint32_t z = 521288629;
static uint32_t w = 88675123;
uint32_t t;
t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
void randxor_init() {}
double randdouble() { return hrandom.dblRandom(); }
bool randbool(const float &p = 0.5) {
return hrandom.uintRandom_65535() < (p * 65535);
}
inline int rnd(const int n) {
assert(0 <= n);
if (n == 1) {
return 0;
}
return ((hrandom.uintRandom_65535()) * n) >> 16;
}
int rnd(const int &l, const int &r) {
assert(l <= r);
return rnd(r - l) + l;
}
struct GaussianSampler {
vector<double> buf;
size_t idx = 0;
GaussianSampler(size_t table_size = 1 << 10) { init(table_size); }
void init(size_t table_size) {
if (table_size % 2 == 1)
table_size++; // 2個ずつ埋めるので偶数に
buf.resize(table_size);
idx = 0;
const double two_pi = 2.0 * (double)pi;
for (size_t i = 0; i < table_size; i += 2) {
// Box-Muller で2個ずつ生成(初期化時のみコストを払う)
double u1 = max(1e-12, randdouble());
double u2 = randdouble();
double r = sqrt(-2.0 * log(u1));
double th = two_pi * u2;
double z0 = r * cos(th);
double z1 = r * sin(th);
buf[i] = z0;
buf[i + 1] = z1;
}
}
inline double sample(double mu = 0.0, double sigma = 1.0) {
if (idx >= buf.size())
idx = 0; // wrap
return mu + sigma * buf[idx++];
}
};
GaussianSampler gaussian_sampler;
class Parameter {
public:
Parameter() {}
static Parameter from_env() {
return Parameter();
// return Parameter(getenv("START_TEMP") ? stof(getenv("START_TEMP"))
// : START_TEMP);
}
};
Parameter param;
std::default_random_engine engine(42);
// ---------------- ここまでテンプレ、修正しないこと -----------------
constexpr int N = 64, M = 256;
array<int, M> A;
array<int, M> laser2port;
void input() {
int dummy;
cin >> dummy;
rep(i, M) {
cin >> A[i];
A[i]--;
laser2port[A[i]] = i;
}
}
struct AnnealingState {
using Cost = int;
Cost score;
Cost best_score;
double start_sec, end_sec;
double temperature;
array<array<int, N>, N> state;
array<array<int, N>, N> best_state;
// [y, x] に d から入ってくる光がどのポートからか
array<array<array<int, 4>, N>, N> from_port;
// [y, x] の d から出ていく行き先のポート
array<array<array<int, 4>, N>, N> to_port;
array<int, M> B;
AnnealingState(double end_sec)
: start_sec(calc_seconds()), end_sec(end_sec) {
init();
}
void init() {
build_initial();
// TODO: 最小化か最大化かで、best_scoreの初期値を変える
// この例は最小化の場合
best_score = inf;
update_best();
}
void build_initial() {
rep(i, M) { update_from_port(i); }
score = calc_score();
}
void update_from_port(int port_idx, bool reset = false) {
int y, x, d;
if (port_idx < N) {
y = 0, x = port_idx, d = 0;
} else if (port_idx < 2 * N) {
y = port_idx - N, x = N - 1, d = 1;
} else if (port_idx < 3 * N) {
y = N - 1, x = 3 * N - 1 - port_idx, d = 2;
} else {
y = 4 * N - 1 - port_idx, x = 0, d = 3;
}
vector<tuple<int, int, int>> yxd;
while (in(y, 0, N) && in(x, 0, N)) {
if (!reset) {
from_port[y][x][d] = port_idx;
} else {
from_port[y][x][d] = -1;
}
d = change_dir(d, state[y][x]);
yxd.emplace_back(y, x, (d + 2) % 4);
y += dy[(d + 2) % 4];
x += dx[(d + 2) % 4];
}
int to_port_idx = get_port_idx(y, x);
if (!reset) {
B[to_port_idx] = A[port_idx];
} else {
B[to_port_idx] = -1;
}
for (auto [y, x, d] : yxd) {
if (!reset) {
to_port[y][x][d] = to_port_idx;
} else {
to_port[y][x][d] = -1;
}
}
}
int calc_score() {
int ans = 0;
rep(i, M) {
int x = B[i];
rep(j, i) {
if (B[j] > x) {
ans++;
}
}
}
return ans;
}
int get_port_idx(int y, int x) {
if (y == -1) {
return x;
}
if (x == N) {
return y + N;
}
if (y == N) {
return 3 * N - 1 - x;
}
if (x == -1) {
return 4 * N - 1 - y;
}
assert(false);
}
// d から入ってきたやつは、次のやつのどれに入るか
int change_dir(int d, int s) {
if (s == 0) {
return d;
}
if (s == 1) {
switch (d) {
case 0:
return 1;
case 1:
return 0;
case 2:
return 3;
default:
return 2;
}
}
if (s == 2) {
switch (d) {
case 0:
return 3;
case 1:
return 2;
case 2:
return 1;
default:
return 0;
}
}
assert(false);
}
void solve() {
// TODO: 温度の設定を変える
double start_temp = 1.0, end_temp = 0.1;
rep(try_num, inf) {
if (try_num % 16 == 0) {
double sec = calc_seconds();
if (should_stop(try_num, sec)) {
DEBUG(try_num);
break;
}
double rate = decide_rate(try_num, sec);
set_temperature(rate, start_temp, end_temp);
}
step();
}
}
inline bool should_stop(int try_num, double sec) const {
#ifdef FREEZE
return try_num > 10000000;
#else
return sec > end_sec;
#endif
}
double decide_rate(int try_num, double sec) const {
#ifdef FREEZE
// フリーズしている場合は、10000回に1回の確率で温度を下げる
return try_num / 10000000.0;
#else
return (sec - start_sec) / (end_sec - start_sec);
#endif
}
void set_temperature(double rate, double start_temp, double end_temp) {
// temperature = start_temp + (end_temp - start_temp) * rate;
temperature = start_temp * pow(end_temp / start_temp, rate);
}
void step() {
int y = rnd(N), x = rnd(N);
int s = rnd(3);
if (state[y][x] == s) {
return;
}
int ori_s = state[y][x];
// (入ってくるポート, 今の出口ポート) と、変更後の出口ポート
array<int, 4> in_from, ori_to, new_to;
int cnt = 0;
rep(d, 4) {
int p = from_port[y][x][d];
if (p == -1) {
continue;
}
in_from[cnt] = p;
// change_dir の戻り値 +2 が、このマスから出ていく辺
ori_to[cnt] = to_port[y][x][(change_dir(d, ori_s) + 2) % 4];
new_to[cnt] = to_port[y][x][(change_dir(d, s) + 2) % 4];
cnt++;
}
bool distinct = cnt == 4;
rep(i, cnt) rep(j, i) {
if (in_from[i] == in_from[j]) {
distinct = false;
}
}
if (!distinct) {
// 4 辺が 4 つの異なるポートに直結していないケース:
// - 同じ経路がこのマスを 2 回通る (外側で辺 b-c がつながっている)
// - 片方の通路がループ上 (from_port == -1)
// - 両方の通路がループ上
// いずれも外側で「ポートにつながる辺」は高々 2 つで、
// 残りの 2 辺は外側で互いにつながっている。
// どう繋ぎ変えても、ポートにつながる 2 辺は (ループを経由するか
// 直接かの違いだけで) 互いにつながるので、出口の対応は不変。
// => スコア変化 0 で、経路の形だけ変わる中立な遷移として常に採用する。
apply_change(y, x, s, in_from, cnt);
return;
}
rep(i, 4) { B[new_to[i]] = A[in_from[i]]; }
Cost new_score = calc_score();
Cost score_diff = new_score - score;
if (should_accept(score_diff)) {
apply_change(y, x, s, in_from, cnt);
score = new_score;
update_best();
return;
}
rep(i, 4) { B[ori_to[i]] = A[in_from[i]]; }
}
// (y, x) を s に変更し、そこを通る経路を引き直す
void apply_change(int y, int x, int s, const array<int, 4> &ports,
int cnt) {
rep(i, cnt) { update_from_port(ports[i], true); }
state[y][x] = s;
// 新しい経路は、旧経路のほかにループだった部分を取り込むことがあるが、
// ポートから引き直せば全部書き込まれる
rep(i, cnt) { update_from_port(ports[i]); }
}
bool should_accept(Cost score_diff) const {
// TODO: 最小化か最大化かで、expの中の符号を変える
// この例は最小化の場合
if (score_diff <= 0) {
return true;
}
return randdouble() < exp(-score_diff / temperature);
}
bool update_best() {
// TODO: 最小化か最大化かで、best_scoreの比較を変える
if (score < best_score) {
best_score = score;
best_state = state;
DEBUG(best_score);
return true;
}
return false;
}
};
signed main(int argc, char **argv) {
input();
AnnealingState anneal(TL);
anneal.solve();
DEBUG(anneal.best_score);
rep(i, N) {
rep(j, N) { cout << anneal.best_state[i][j]; }
cout << endl;
}
}
fuppy_kyopro