// Astraに作ってもらった解法 // NOTE: Writer解のスコアが順位表に現れないように、あえてスコア最小化としている // 問題の変換:鏡の配置を直接探索せず、160個のポートを80組のペアに分ける問題として考える。 // 実現可能性の判定:盤面を分割する3,042個の切断条件を使い、各切断をまたぐペア数が通路容量を超えないようにする。 // 近傍探索:2~6組のペアを組み替え、実現可能性を維持しながら、公式スコアを減らす。 // 焼きなまし法:スコアが悪化する変更も一定確率で採用し、局所最適解を脱出しながら探索する。 // 鏡の配置を構築:最良のペア構成から、平面グラフの経路構成アルゴリズムで光路を作り、各マスの鏡の向きを決定する。 // Spectrum Router: feasible perfect-matching search + exact planar routing. // Build: g++ -std=c++17 -O3 -DNDEBUG main.cpp -o solver // Run: ./solver < input.txt > output.txt // The default wall-clock search budget is 1.45 seconds (for a 2-second limit). // // Routing reference: D. Wagner and K. Weihe, // "A linear-time algorithm for edge-disjoint paths in planar graphs", // Combinatorica 15(1), 135-150 (1995). // https://math.mit.edu/~goemans/18438F09/wagnerweihe.pdf #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Clock = chrono::steady_clock; struct Random { uint64_t state = 2; uint64_t next() { state ^= state << 7; state ^= state >> 9; return state; } int below(int n) { return int(next() % uint64_t(n)); } double unit() { return double(next() >> 11) * 0x1.0p-53; } }; struct Options { double seconds = 1.45; uint64_t iterations = 0, seed = 1; int cycles = 6; bool large_moves = true; }; Options parse_options(int argc, char** argv) { Options o; for (int i = 1; i < argc; ++i) { string arg = argv[i]; auto value = [&]() -> string { if (++i >= argc) throw invalid_argument("Missing option value"); return argv[i]; }; if (arg == "--time") o.seconds = stod(value()); else if (arg == "--iterations") o.iterations = stoull(value()); else if (arg == "--seed") o.seed = stoull(value()); else if (arg == "--cycles") o.cycles = stoi(value()); else if (arg == "--no-large-moves") o.large_moves = false; else throw invalid_argument("Unknown option: " + arg); } if (!isfinite(o.seconds) || o.seconds <= 0 || o.cycles < 1) throw invalid_argument("Invalid time budget or cycle count"); return o; } // A half-edge h=4*cell+side uses U=0, R=1, D=2, L=3. // The two connections inside a cell are h <-> h XOR key[state]. struct Grid { int n, m, cells; vector neighbor, entrance; static constexpr int keys[3] = {2, 3, 1}; explicit Grid(int size) : n(size), m(4*n), cells(n*n), neighbor(4*cells), entrance(m) { constexpr int dr[4] = {-1,0,1,0}, dc[4] = {0,1,0,-1}; for (int r=0; r=n) p=n+r; else if (rr>=n) p=3*n-1-c; else if (cc<0) p=4*n-1-r; if (p>=0) { neighbor[h]=-p-1; entrance[p]=h; } else neighbor[h]=4*(rr*n+cc)+(d^2); } } int trace(const vector& board, int port) const { int h=entrance[port], steps=0; while (h>=0) { h=neighbor[h ^ keys[board[h/4]]]; if (++steps>4*cells) throw runtime_error("Trace failed to exit"); } return -h-1; } // Return an empty vector exactly when the requested pairing cannot be routed. // Phase 1 routes the noncrossing parenthesis matching and orients its edges. // Phase 2 routes the original pairs in the resulting directed graph. vector route(const vector& partner) const { vector orientation(4*cells,0); vector used(4*cells,0); vector stack, closing, board(cells,0); for (int t=m-1; t>=0; --t) { if (partner[t]=0) { int out=-1; for (int d=1; d<4; ++d) { int v=(h&~3) | ((h-d)&3); if (!orientation[v]) { out=v; break; } } if (out<0) return {}; orientation[out]=1; h=neighbor[out]; if (h>=0) orientation[h]=-1; } if (-h-1!=t) return {}; } for (int t : closing) { int h=entrance[partner[t]]; used[h]=1; while (h>=0) { int out=-1; for (int d=1; d<4; ++d) { int v=(h&~3) | ((h-d)&3); if (orientation[v]==1 && !used[v]) { out=v; break; } } if (out<0) return {}; int k=h^out; board[h/4]=(k==2 ? 0 : k==3 ? 1 : 2); used[out]=1; h=neighbor[out]; if (h>=0) used[h]=1; } if (-h-1!=t) return {}; } // Always verify the returned board, independently of the cut data. for (int p=0; p> member; vector tight; vector slack; void apply_bits(int word, uint64_t inc, uint64_t dec) { while (inc) { int bit=__builtin_ctzll(inc), id=64*word+bit; if (--slack[id]==0) tight[word] |= uint64_t(1)<(words,0)), tight(words,0), slack(count) { int id=0; for (int axis=0; axis<2; ++axis) for (int t=1; t& partner) { fill(tight.begin(),tight.end(),0); int id=0; for (int axis=0; axis<2; ++axis) for (int t=1; t>bit)&1; int s=n+abs(t-b)-crossing; if (s<0 || (s&1)) throw runtime_error("Invalid cut slack"); slack[id]=s/2; if (!s) tight[word] |= uint64_t(1)<>bit)&1)+2*int((diff[1]>>bit)&1)+ 4*int((diff[2]>>bit)&1)-8*int((diff[3]>>bit)&1); if (!apply) { if (slack[id]& b) { int result=0; for (int i=0; ib[j]; return result; } // Change b in place and return the EXACT change in its inversion count. int swap_delta(vector& b,int i,int j) { if (i>j) swap(i,j); int x=b[i],y=b[j],lo=min(x,y),hi=max(x,y),between=0; for (int t=i+1; tlo && b[t](Clock::now()-start).count(); }; int n; if (!(cin>>n) || n<1 || n>100) throw invalid_argument("N must be in 1..100"); Grid grid(n); int m=grid.m; vector a(m),seen(m,0); for (int& v:a) { if (!(cin>>v) || v<1 || v>m || seen[v-1]++) throw invalid_argument("A must be a permutation of 1..4N"); --v; } Random rng; rng.state=opt.seed+1; if (!rng.state) rng.state=1; vector partner(m),b(m),empty(grid.cells,0); for (int p=0; p best_partner=partner; CutConstraints cuts(n); cuts.reset(partner); vector> candidates(m); for (int p=0; p=opt.seconds || (opt.iterations && iteration>=opt.iterations)) break; progress=opt.iterations ? double(iteration)/opt.iterations : now/opt.seconds; int cycle=min(opt.cycles-1,int(progress*opt.cycles)); double fraction=progress*opt.cycles-cycle; if (cycle!=phase) { phase=cycle; if (cycle) { partner=best_partner; for (int p=0;p3 ? 12 : 24))]; else q=rng.below(m); x[j]=partner[q]; y[j]=q; for (int t=0;t=0 || rng.unit() board=grid.route(best_partner); if (board.empty()) throw runtime_error("Routing unexpectedly failed"); vector verified(m); for (int p=0;p