// tester解 // 解説スライドのStep6相当の解法ですが、細かい部分は一致していません。 #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include using namespace std; int dx[4] = { 0,1,0,-1 }; int dy[4] = { 1,0,-1,0 }; int ndx[8] = { 0,1,0,-1, 1,1,-1,-1 }; int ndy[8] = { 1,0,-1,0, 1,-1,-1,1 }; double pi = 3.141592653589793; std::mt19937 mt_for_action(0); // 時間をDouble型で管理し、経過時間も取り出せるクラス class TimeKeeperDouble { private: std::chrono::high_resolution_clock::time_point start_time_; double time_threshold_; double now_time_ = 0; public: // 時間制限をミリ秒単位で指定してインスタンスをつくる。 TimeKeeperDouble(); TimeKeeperDouble(const double time_threshold) : start_time_(std::chrono::high_resolution_clock::now()), time_threshold_(time_threshold) { } // 経過時間をnow_time_に格納する。 void setNowTime() { auto diff = std::chrono::high_resolution_clock::now() - this->start_time_; this->now_time_ = std::chrono::duration_cast(diff).count() * 1e-3; // ms } // 経過時間をnow_time_に取得する。 double getNowTime() const { return this->now_time_; } // インスタンス生成した時から指定した時間制限を超過したか判定する。 bool isTimeOver() const { return now_time_ >= time_threshold_; } }; int TIME_THR = 1900; int N, M; vector a; // 座標クラス class Coord { public: int x, y; Coord() { x = 0, y = 0; } Coord(int _x, int _y) { x = _x, y = _y; } void move(int dir) { x += dx[dir]; y += dy[dir]; } }; // 盤面 class Board { public: // 鏡の配置 vector> s; // 各出力ポートが受け取る光の番号 vector b; // 転倒数 int inversion; // マスを通る光のセット through_lights[x][y][k]: (x,y)に方向kで入ってくる光の出力ポート vector>> through_lights; Board() { s.resize(N, vector(N, 0)); set_b(); set_inversion(); } void print() { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << s[i][j]; } cout << endl; } } // 入力ポート番号を指定して、始めの方向と座標を設定 void set_start_status(int in_port, int& dir, Coord& coord) { if (in_port < N) { dir = 1; coord = Coord(0, in_port); } else if (in_port < 2 * N) { dir = 2; coord = Coord(in_port % N, N - 1); } else if (in_port < 3 * N) { dir = 3; coord = Coord(N - 1, N - 1 - in_port % N); } else { dir = 0; coord = Coord(N - 1 - in_port % N, 0); } } // 出力後の座標を指定して、出力ポート番号を設定 void set_out_ports(int& out_port, Coord& coord) { if (coord.x == -1) { out_port = coord.y; } else if (coord.y == N) { out_port = N + coord.x; } else if (coord.x == N) { out_port = N * 2 + (N - 1 - coord.y); } else { out_port = N * 3 + (N - 1 - coord.x); } } // あるマスに方向kで入った光が、どの方向に反射するのかを計算 int reflect_dir(int k, int s) { if (s == 1) { // / if (k == 0) return 3; else if (k == 1) return 2; else if (k == 2) return 1; else if (k == 3) return 0; } if (s == 2) { // \ // if (k == 0) return 1; else if (k == 1) return 0; else if (k == 2) return 3; else if (k == 3) return 2; } return k; } // ポートin_portから出た光をシミュレート void simulate_light(int in_port, vector>& cells, int& out_port) { // ポートiから出た光がどこに行くのかを調査 int dir; Coord coord; set_start_status(in_port, dir, coord); while (0 <= coord.x && coord.x < N && 0 <= coord.y && coord.y < N) { cells.push_back(make_pair(coord, dir)); dir = reflect_dir(dir, s[coord.x][coord.y]); coord.move(dir); } set_out_ports(out_port, coord); } // b(受信番号列)を計算 void set_b() { // 初期化 b.resize(M, -1); through_lights.resize(N, vector>(N, vector(4))); for (int in_port = 0; in_port < M; in_port++) { // in_portから出た光を辿るときに、通るマスと侵入方向 vector> cells; // 出力ポート int out_port; simulate_light(in_port, cells, out_port); b[out_port] = a[in_port]; for (auto cell : cells) { through_lights[cell.first.x][cell.first.y][cell.second] = out_port; } } } //// 転倒数を計算 void set_inversion() { inversion = 0; for (int i = 0; i < M; i++) { for (int j = i + 1; j < M; j++) { if (b[i] > b[j]) { inversion++; } } } } // b(出力された番号)の2点をSwapしたときの転倒数差分を計算 int calc_b_swap_inversion_diff(int id0, int id1) { if (id0 > id1) swap(id0, id1); int diff = 0; if (b[id0] < b[id1]) { diff++; } else { diff--; } for (int i = id0 + 1; i < id1; i++) { if (b[id0] < b[i]) { diff++; } else { diff--; } if (b[i] < b[id1]) { diff++; } else { diff--; } } return diff; } // (x,y)の鏡をsに変えたときのスコア増加量を計算 int calc_change_score_diff(int x, int y, int s_new) { int s_old = s[x][y]; // 鏡の変更によりswapされるペア vector> swap_out_port; // どのように鏡を変えたかで、出力ポートのどのペアがswapされるか if ((s_old == 0 && s_new == 1) || (s_old == 1 && s_new == 0)) { swap_out_port.push_back(make_pair(through_lights[x][y][0], through_lights[x][y][3])); swap_out_port.push_back(make_pair(through_lights[x][y][1], through_lights[x][y][2])); } if ((s_old == 0 && s_new == 2) || (s_old == 2 && s_new == 0)) { swap_out_port.push_back(make_pair(through_lights[x][y][0], through_lights[x][y][1])); swap_out_port.push_back(make_pair(through_lights[x][y][2], through_lights[x][y][3])); } if ((s_old == 1 && s_new == 2) || (s_old == 2 && s_new == 1)) { swap_out_port.push_back(make_pair(through_lights[x][y][0], through_lights[x][y][2])); swap_out_port.push_back(make_pair(through_lights[x][y][1], through_lights[x][y][3])); } // swapされるペアごとに転倒数がどう変わるかを計算 int diff = 0; for (auto pair : swap_out_port) { if (pair.first == pair.second) { continue; } if (pair.first == -1 || pair.second == -1) { continue; } diff += calc_b_swap_inversion_diff(pair.first, pair.second); swap(b[pair.first], b[pair.second]); } // bを更新した分を戻す for (auto pair : swap_out_port) { if (pair.first == -1 || pair.second == -1) { continue; } swap(b[pair.first], b[pair.second]); } return diff; } // (x,y)の鏡をsに変更 void change(int x, int y, int s_new, int diff) { auto in_ports = through_lights[x][y]; inversion += diff; // through_lights更新のため、古い鏡の配置でシミュレーション for (auto in_port : in_ports) { if (in_port == -1) { continue; } // in_portから出た光を辿るときに、通るマスと侵入方向 vector> cells; // 出力ポート int out_port; simulate_light(in_port, cells, out_port); for (auto cell : cells) { through_lights[cell.first.x][cell.first.y][cell.second] = -1; } } // 鏡の更新 s[x][y] = s_new; // 更新後の鏡で再度シミュレーション for (auto in_port : in_ports) { if (in_port == -1) { continue; } // in_portから出た光を辿るときに、通るマスと侵入方向 vector> cells; // 出力ポート int out_port; simulate_light(in_port, cells, out_port); b[out_port] = a[in_port]; for (auto cell : cells) { through_lights[cell.first.x][cell.first.y][cell.second] = out_port; } } } }; Board solve() { auto board = Board(); auto best_board = board; auto time_keeper = TimeKeeperDouble(TIME_THR); int cnt = 0; int accept = 0; double start_temp = 2; double end_temp = 0; while (1) { time_keeper.setNowTime(); if (time_keeper.isTimeOver()) { break; } cnt++; // 近傍生成 int i = mt_for_action() % N; int j = mt_for_action() % N; int new_s = mt_for_action() % 3; int old_s = board.s[i][j]; if (new_s == old_s) { continue; } // スコア差分を計算 int diff = board.calc_change_score_diff(i, j, new_s);; // 遷移判定 double temp = +start_temp + +(end_temp - start_temp) * (time_keeper.getNowTime() / TIME_THR); int big_M = 100000000; double rand = double(mt_for_action() % big_M) / big_M; if (exp(-1 * diff / temp) > rand) { board.change(i, j, new_s, diff); accept++; } if (best_board.inversion > board.inversion) { best_board = board; } } cerr << "cnt: " << cnt << endl; cerr << "accept: " << accept << endl; cerr << "inversion: " << best_board.inversion << endl; cerr << "score: " << M * (M - 1) / 2 - best_board.inversion << endl; return best_board; } int main() { cin >> N; M = N * 4; a.resize(M); for (int i = 0; i < M; i++) { cin >> a[i]; } auto ans = solve(); ans.print(); }