#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; } 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 best = calc_score(out); cerr << "Initial best score: " << best << endl; int ti=clock(); 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); out[i][j] = '0' + type; //cerr << "Trying type " << type << " with positions (" << i << "," << j << ") and (" << i2 << "," << j2 << ")" << endl; int score = calc_score(out); //cerr << "Current score: " << score << endl; if(score >= best||zero_one_dist(mt) < exp((score-best) / current_temperature)){ if(score > best){ cerr << "New best score: " << score << endl; best = score; best_out = out; } }else{ // 元に戻す out[i][j] = '0' + old; // 元に戻す } //cerr<<__LINE__<<"-----"<> n; rep(i, M) cin >> a[i]; vector out = solve(); //cerr<<__LINE__<<"-----"<