// 4. #include #include #include #include #include #include #include #include #include #include #include #define rep(i,a,b) for(int i=a;i #define vvi vector #define vs vector template bool chmin(T &a, const T& b){if(a>b){a=b; return true;}return false;} template bool chmax(T &a, const T& b){if(a; template using min_priority_queue = priority_queue, greater>; //const long long INF=1e18; const int INF=1000000000; const int64_t CYCLES_PER_SEC = 2900000000;//3000000000; const double TIMELIMIT = 1.95; constexpr int RANDOM_SEED = 42; // 乱数のシード値 const int dx[]={-1,0,1,0}; // 上,左,下,右 const int dy[]={0,-1,0,1}; bool is_outer_range(int R1,int r1,int R2,int r2){ return (r1<0 or R1<=r1 or r2<0 or R2<=r2); } mt19937 mt(RANDOM_SEED); uniform_int_distribution<> rand3(0,2); // [0,2]を出力 uniform_int_distribution<> rand64(0,63); // [0,2]を出力 struct Input{ int N; vi A; Input(int N=0, vi A={}): N(N), A(A) {} }; struct Output{ vs S; Output(vs S={}): S(S) {} }; struct Solver { // グローバル変数============================== std::chrono::system_clock::time_point start_time; Input input; Output best_ans; ll best_score; // 便利関数============================== // 乱数 double Randouble(){ return 1.0 * rand() / RAND_MAX; } // スコア計算 ll calc_score(const vector& a, const vector& board) { int n = board.size(); int m = 4 * n; // b[i]: 出口iに到達したレーザーの値 vector b(m, 0); // 上=0、右=1、下=2、左=3 const int dr[4] = {-1, 0, 1, 0}; const int dc[4] = {0, 1, 0, -1}; // 各入口からレーザーを追跡 rep(i,0,m) { int r, c, d; // 入口の座標と進行方向 if (i < n) { r = 0; c = i; d = 2; } else if (i < 2 * n) { r = i - n; c = n - 1; d = 3; } else if (i < 3 * n) { r = n - 1; c = 3 * n - 1 - i; d = 0; } else { r = 4 * n - 1 - i; c = 0; d = 1; } // レーザーを進める rep(step,0,4*n*n) { char cell = board[r][c]; // 鏡による反射 if (cell == '1') d ^= 1; if (cell == '2') d ^= 3; // 次のマスへ r += dr[d]; c += dc[d]; // 盤面から出た場合 if (r < 0 || r >= n || c < 0 || c >= n) { int exit; if (r < 0) { exit = c; } else if (c >= n) { exit = n + r; } else if (r >= n) { exit = 3 * n - 1 - c; } else { exit = 4 * n - 1 - r; } b[exit] = a[i]; break; } } } // 転倒数を計算 ll inversions = 0; rep(i,0,m) { rep(j,i+1,m) { if (b[i] > b[j]) { inversions++; } } } // 得点 = 全ペア数 - 転倒数 // ここで返すのは転倒数にする ll score = inversions; return score; } // 初期解を生成する Output get_initial_solution(){ vs S(input.N,string(input.N,'0')); return {S}; } // pre_ansからnew_ansを作る(ランダム変更) Output make_new_ans(Output &pre_ans){ // コピー Output new_ans=pre_ans; // 隣接を求める int row=rand64(mt); int col=rand64(mt); int nx=rand3(mt); while(nx==new_ans.S[row][col]-'0') nx=rand3(mt); new_ans.S[row][col]=nx+'0'; return new_ans; } // 初期解生成 void init(){ Output ans=get_initial_solution(); best_ans=ans; best_score=calc_score(input.A, best_ans.S); } void SA(){ Output pre_ans=best_ans; best_score = calc_score(input.A, pre_ans.S); ll pre_score = best_score; double kankaku = TIMELIMIT / 10 * 1000; // msオーダー double ne = 0; int cnt = 0; int cnt2 = 0; double start_temp = 5; double end_temp = 1; while(1){ // 時間計測 std::chrono::system_clock::time_point end = std::chrono::system_clock::now(); // 計測終了時間 double elapsed = std::chrono::duration_cast(end-start_time).count(); //処理に要した時間をミリ秒に変換 if(TIMELIMIT*1000 < elapsed) break; double temp = start_temp + (end_temp - start_temp) * elapsed / TIMELIMIT / 1000; if (elapsed> ne) { ne += kankaku; cerr << temp << " " << pre_score << " " << best_score << endl; } // // 新たな解の生成============================== // Output new_ans; // int w=rand10000(mt)%2; // if(w==0){ // new_ans=make_new_ans1(pre_ans); // }else{ // new_ans=make_new_ans2(pre_ans); // } Output new_ans=make_new_ans(pre_ans); // Output new_ans=pre_ans; // new_scoreを計算 ll new_score=calc_score(input.A, new_ans.S); // スコア計算 // 採用するか否かを決定(scoreは小さいほど良い) // if(new_score 10000 * (rand() % 10000)) { if (new_score < pre_score) { pre_score=new_score; pre_ans=new_ans; if(chmin(best_score,new_score)){ best_score=new_score; best_ans=new_ans; } cnt2++; } cnt++; } // dump(cnt); // dump(cnt2); //ti = timer.get(); //cerr << ti << endl; } void input_func() { start_time = std::chrono::system_clock::now(); // 計測開始時間 cin.tie(0); ios::sync_with_stdio(false); int N,M; cin >> N; M=N*4; vi A(M); rep(i,0,M) cin >> A[i]; input={N,A}; } int solve() { init(); SA(); return 0; } void output(){ cerr << best_score << endl; // cerr << round(1000000LL*1000000LL/(best_score+1)) << endl; rep(i,0,input.N) cout << best_ans.S[i] << endl; // デバッグ // double ti = timer.get(); // cerr << ti-start_time << endl; } }; Solver solver; int main(int argc, char *argv[]) { solver.input_func(); solver.solve(); solver.output(); return 0; }