#include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; using lint = long long int; using P = pair; using PL = pair; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define ALL(a) (a).begin(),(a).end() constexpr int MOD = 1000000007; vector RH_B = {1532834020, 1388622299}; vector RH_M = {2147482409, 2147478017}; constexpr int INF = 2147483647; void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";} templatevoid chmax(T &a, const T &b) { if (avoid chmin(T &a, const T &b) { if (b A; vector dx = {-1, 0, 1, 0}; vector dy = {0, 1, 0, -1}; vector dc = {'U', 'R', 'D', 'L'}; mt19937 mt(0); void set_B(int idx, vector &B, vector &light_id) { if(idx < N) { B[idx] = light_id[0*N*4 + idx*4 + 0]; } else if(idx < 2*N) { B[idx] = light_id[(idx-N)*N*4 + (N-1)*4 + 1]; } else if(idx < 3*N) { B[idx] = light_id[(N-1)*N*4 + (N-1 - (idx-2*N))*4 + 2]; } else if(idx < 4*N) { B[idx] = light_id[(N-1 - (idx-3*N))*N*4 + 0*4 + 3]; } } int calc_score(vector &B) { //転倒数を最小化 int score = M*(M-1) / 2; REP(i, M) FOR(j, i+1, M) if(B[i] > B[j]) score--; return score; } int calc_score_easy(vector &B) { int score = 0; REP(i, M) score += abs(i - B[i]); return M * (M-1) / 2 - score; } vector get_B(vector &grid) { vector B(M); //全てのポートから発射される光を順番に追跡する(計算量N*N*4) REP(port, M) { int x, y, d; if(port < N) { x = 0; y = port; d = 2; } else if(port < 2*N) { x = port - N; y = N-1; d = 3; } else if(port < 3*N) { x = N-1; y = N-1 - (port - 2*N); d = 0; } else { x = N-1 - (port - 3*N); y = 0; d = 1; } while(x >= 0 && x < N && y >= 0 && y < N) { if(grid[x][y] == '1') { if(d == 0) d = 1; else if(d == 1) d = 0; else if(d == 2) d = 3; else if(d == 3) d = 2; } else if(grid[x][y] == '2') { if(d == 0) d = 3; else if(d == 1) d = 2; else if(d == 2) d = 1; else if(d == 3) d = 0; } int nx = x + dx[d]; int ny = y + dy[d]; x = nx; y = ny; } if(x == -1) B[y] = A[port]; else if(y == N) B[x + N] = A[port]; else if(x == N) B[3*N-1 - y] = A[port]; else if(y == -1) B[4*N-1 - x] = A[port]; } return B; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; M = 256; A.resize(M); REP(i, M) cin >> A[i]; REP(i, M) A[i]--; //そのマスでその方向に向かっている光のID vector light_id(N*N*4); //そのマスからその方向に向かったときに到達するポート vector next_port(N*N*4); REP(i, N) REP(j, N) { light_id[i*N*4 + j*4 + 0] = A[3*N-1 - j]; light_id[i*N*4 + j*4 + 1] = A[4*N-1 - i]; light_id[i*N*4 + j*4 + 2] = A[j]; light_id[i*N*4 + j*4 + 3] = A[N + i]; next_port[i*N*4 + j*4 + 0] = j; next_port[i*N*4 + j*4 + 1] = N + i; next_port[i*N*4 + j*4 + 2] = 3*N-1 - j; next_port[i*N*4 + j*4 + 3] = 4*N-1 - i; } vector grid(N, string(N, '0')); vector B = get_B(grid); int best_score = calc_score_easy(B); double time = 0; int loop = 0; double start_temp = 0.8, end_temp = -0.2; double temp = start_temp; auto start_time = std::chrono::system_clock::now(); int best_score_2 = best_score; vector best_grid = grid; int TIME_LIMIT = 1900; while(true) { if(loop%1000 == 0) { auto now_time = std::chrono::system_clock::now(); time = std::chrono::duration_cast(now_time-start_time).count(); temp = start_temp + (end_temp - start_temp) * time / TIME_LIMIT; cerr << "LOOP:" << loop << " SCORE:" << best_score << " TIME:" << time << endl; if(time > TIME_LIMIT) break; } loop++; int i = mt() % N; int j = mt() % N; char old_val = grid[i][j]; char new_val = '0' + (mt() % 3); if(mt() % 4 != 0) new_val = '0'; if(new_val == old_val) continue; //スコアの差分計算 int new_score = best_score; REP(d, 4) if(light_id[i*N*4 + j*4 + d] != -1) new_score += abs(light_id[i*N*4 + j*4 + d] - next_port[i*N*4 + j*4 + d]); grid[i][j] = new_val; //そのマスだけ光の方向を更新する if((old_val == '0' && new_val == '1') || (old_val == '1' && new_val == '0')) { swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 1]); swap(light_id[i*N*4 + j*4 + 2], light_id[i*N*4 + j*4 + 3]); } else if(old_val == '0' && new_val == '2' || old_val == '2' && new_val == '0') { swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 3]); swap(light_id[i*N*4 + j*4 + 1], light_id[i*N*4 + j*4 + 2]); } else if(old_val == '1' && new_val == '2' || old_val == '2' && new_val == '1') { swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 2]); swap(light_id[i*N*4 + j*4 + 1], light_id[i*N*4 + j*4 + 3]); } REP(d, 4) if(light_id[i*N*4 + j*4 + d] != -1) new_score -= abs(light_id[i*N*4 + j*4 + d] - next_port[i*N*4 + j*4 + d]); double prob = exp(((double)new_score-best_score)/pow(10, temp)); if(new_score > best_score || prob > (mt()%INF)/(double)INF) { best_score = new_score; //light_idとnext_portを再構築する必要がある //i,jから4方向を探索する REP(d, 4) { int ci = i, cj = j, cd = d; int light = light_id[ci*N*4 + cj*4 + cd]; int mirror_d = (d + 2) % 4; if(grid[ci][cj] == '1') { if(cd == 0) mirror_d = 3; else if(cd == 1) mirror_d = 2; else if(cd == 2) mirror_d = 1; else if(cd == 3) mirror_d = 0; } else if(grid[ci][cj] == '2') { if(cd == 0) mirror_d = 1; else if(cd == 1) mirror_d = 0; else if(cd == 2) mirror_d = 3; else if(cd == 3) mirror_d = 2; } int port = next_port[ci*N*4 + cj*4 + mirror_d]; int patience = 256; int last_d = cd; while(ci >= 0 && ci < N && cj >= 0 && cj < N) { ci += dx[cd]; cj += dy[cd]; last_d = cd; patience--; if(patience == 0) { light = -1; port = -1; } else if(patience == -256) { break; } if(ci < 0 || ci >= N || cj < 0 || cj >= N) break; next_port[ci*N*4 + cj*4 + (last_d + 2) % 4] = port; if(grid[ci][cj] == '1') { if(cd == 0) cd = 1; else if(cd == 1) cd = 0; else if(cd == 2) cd = 3; else if(cd == 3) cd = 2; } else if(grid[ci][cj] == '2') { if(cd == 0) cd = 3; else if(cd == 1) cd = 2; else if(cd == 2) cd = 1; else if(cd == 3) cd = 0; } light_id[ci*N*4 + cj*4 + cd] = light; } } if(new_score > best_score_2) { best_score_2 = new_score; best_grid = grid; } } else { grid[i][j] = old_val; if((old_val == '0' && new_val == '1') || (old_val == '1' && new_val == '0')) { swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 1]); swap(light_id[i*N*4 + j*4 + 2], light_id[i*N*4 + j*4 + 3]); } else if((old_val == '0' && new_val == '2') || (old_val == '2' && new_val == '0')) { swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 3]); swap(light_id[i*N*4 + j*4 + 1], light_id[i*N*4 + j*4 + 2]); } else if((old_val == '1' && new_val == '2') || (old_val == '2' && new_val == '1')) { swap(light_id[i*N*4 + j*4 + 0], light_id[i*N*4 + j*4 + 2]); swap(light_id[i*N*4 + j*4 + 1], light_id[i*N*4 + j*4 + 3]); } } } REP(i, N) cout << best_grid[i] << "\n"; vector final_B = get_B(best_grid); cerr << calc_score(final_B) << "\n"; }