#include // cout, endl, cin #include // string, to_string, stoi #include // vector #include // min, mai, swap, sort, reverse, lower_bound, upper_bound #include // pair, make_pair #include // tuple, make_tuple #include // int64_t, int*_t #include // printf #include // map #include // queue, priority_queue #include // set #include // stack #include // deque #include // unordered_map #include // unordered_set #include // bitset #include // isupper, islower, isdigit, toupper, tolower #include // fiied,setprecision #include // INT_MAi #include // M_PI #include // random_device, mt19937, uniform_int_distribution #include // 正規表現 #include // time #include // ifstream, ofstream #include // array #include // bit #include // chrono #include // span #include // sqrt, sin, cos, tan, asin, acos, atan, atan2 #include // 複素数 // #include using namespace std; #include 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; const int INF = 1001001001; //----------------------------------------------------- // 乱数生成器 std::mt19937 mt{42}; std::uniform_real_distribution 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 a(M),b(M); int calc_score(const vector& 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& 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& out, int x, int y, int type, array, 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 solve() { //cerr << "Starting solve" << endl; vector 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'; } } vectortate(N, false), yoko(N, false); vectororder_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, 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__<<"-----"<> n; rep(i, M) cin >> a[i]; vector out = solve(); //cerr<<__LINE__<<"-----"<