// #pragma GCC optimize("Ofast") #include using namespace std; #include using namespace atcoder; using ll = long long; using vecint = std::vector; using vecll = std::vector; using vecstr = std::vector; using vecbool = std::vector; using vecdou = std::vector; using vecpl = std::vector>; using vec2d = std::vector; using pl = pair; using pint = pair; #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define rep1(i,n) for (int i = 1; i <= (int)(n); i++) #define all(a) (a).begin(), (a).end() #define INF 1000000000 #define inr(l,x,r) ((l) <= (x) && (x) < (r)) class TimeKeeperDouble { private: std::chrono::high_resolution_clock::time_point start_time_; double time_threshold_; double now_time_ = 0; public: // 時間制限をミリ秒単位で指定してインスタンスをつくる。 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 } void changeTimeThreshold(const double time_threshold) { this->time_threshold_ = time_threshold; } // 経過時間をnow_time_に取得する。 double getNowTime() const { return this->now_time_; } // インスタンス生成した時から指定した時間制限を超過したか判定する。 bool isTimeOver() const { return now_time_ >= time_threshold_; } }; #include using namespace std; class Random { private: uint32_t x2_; uint32_t x3_; uint64_t c_x1_; static uint64_t splitmix64(uint64_t& x) { x += 0x9e3779b97f4a7c15ULL; uint64_t z = x; z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; return z ^ (z >> 31); } public: // seedを指定して初期化 Random(const uint64_t seed = 0) { setSeed(seed); } inline void setSeed(const uint64_t seed = 0) { if (seed == 0) { x2_ = 12345U; x3_ = 0xcafef00dU; c_x1_ = (0xd15ea5e5ULL << 32) | 23456ULL; return; } uint64_t x = seed; x2_ = static_cast(splitmix64(x)); x3_ = static_cast(splitmix64(x)); c_x1_ = splitmix64(x); } inline uint32_t next() { const uint64_t x = static_cast(x3_) * 3487286589ULL; const uint32_t ret = (x3_ ^ x2_) + (static_cast(c_x1_) ^ static_cast(x >> 32)); x3_ = x2_; x2_ = static_cast(c_x1_); c_x1_ = x + (c_x1_ >> 32); return ret; } inline uint64_t next64() { return (static_cast(next()) << 32) | next(); } inline double nextf() { const uint64_t bits = 0x3ff0000000000000ULL | (static_cast(next()) << 20); double ret; memcpy(&ret, &bits, sizeof(ret)); return ret - 1.0; } inline size_t get(const size_t n) { assert(0 < n && n <= static_cast(UINT32_MAX)); return (static_cast(next()) * n) >> 32; } inline size_t range(const size_t a, const size_t b) { assert(a < b); return get(b - a) + a; } inline size_t range_skip(const size_t a, const size_t b, const size_t skip) { assert(a <= skip && skip < b); const size_t n = range(a, b - 1); return n + (skip <= n); } inline int64_t rangei(const int64_t a, const int64_t b) { assert(a < b); return static_cast(get(static_cast(b - a))) + a; } template inline void shuffle(vector& a) { for (size_t i = a.size(); i > 1; --i) { swap(a[i - 1], a[get(i)]); } } // 0以上m未満の整数の範囲の乱数 inline int nextInt(const int m) { assert(0 < m); return static_cast(get(static_cast(m))); } // 0以上1.0未満の実数の範囲の乱数 inline double nextDouble() { return nextf(); } // 0以上1.0未満の実数の範囲の乱数のlog。焼きなまし法で使いやすい。 inline double nextLog() { return log(nextDouble()); } inline uint32_t nextUInt32() { return next(); } }; Random rnd(1019); const int N=64; vecint A(4*N); void input() { int tmp; cin >> tmp; for (int i=0; i<4*N; i++) { cin >> A[i]; } } void solve() { vector> U(N,vecint(N)), D(N,vecint(N)), L(N,vecint(N)), R(N,vecint(N)); vector B(4*N); rep(i,N) { U[0][i] = i; D[N-1][i] = 3*N-1-i; L[i][0] = 4*N-1-i; R[i][N-1] = N+i; } vector> ans(N,vecint(N,0)); rep(i,N) { rep(j,N-1) { L[i][j+1] = L[i][j]; U[j+1][i] = U[j][i]; } for (int j=N-1; j>0; j--) { R[i][j-1] = R[i][j]; D[j-1][i] = D[j][i]; } } rep(i,N) { B[i] = A[D[0][i]]; B[N+i] = A[L[i][N-1]]; B[2*N+i] = A[U[N-1][N-1-i]]; B[3*N+i] = A[R[N-1-i][0]]; } ll cost = 0; fenwick_tree ft(4*N+1); // Bの転倒数 for(int i=0;i<4*N;i++){ cost += i-ft.sum(0,B[i]); ft.add(B[i],1); } double T0 = 50.0; double T1 = 0.1; TimeKeeperDouble tk(1900.0); int itr = 1; double T = T0; while (itr%256 || !tk.isTimeOver()) { ++itr; if(itr%256==0) { tk.setNowTime(); T = T0 * pow(T1/T0,tk.getNowTime()/1900.0); } int x = rnd.nextInt(N); int y = rnd.nextInt(N); int dt = rnd.nextInt(2) + 1; int original = ans[x][y]; int new_type = (original+dt)%3; int a,b,c,d; if(original == 0){ if(new_type == 1){ a = U[x][y]; b = D[x][y]; c = R[x][y]; d = L[x][y]; }else if(new_type == 2){ a = U[x][y]; b = D[x][y]; c = L[x][y]; d = R[x][y]; } }else if(original == 1){ if(new_type == 0){ a = U[x][y]; b = L[x][y]; c = R[x][y]; d = D[x][y]; }else if(new_type == 2){ a = U[x][y]; b = L[x][y]; c = D[x][y]; d = R[x][y]; } }else if(original == 2){ if(new_type == 0){ a = U[x][y]; b = R[x][y]; c = L[x][y]; d = D[x][y]; }else if(new_type == 1){ a = U[x][y]; b = R[x][y]; c = D[x][y]; d = L[x][y]; } } if(a==b || a==c || a==d || b==c || b==d || c==d) continue; int cost_diff = 0; if(a>c) swap(a,c); if(b>d) swap(b,d); for(int i=a+1;i update = [&](int xx, int yy, char cc) { if(cc=='U') { if(xx == 0) return; int C; if(ans[xx][yy] == 0) C = D[xx][yy]; else if(ans[xx][yy] == 1) C = L[xx][yy]; else C = R[xx][yy]; D[xx-1][yy] = C; if(ans[xx-1][yy] == 0)update(xx-1,yy,'U'); else if(ans[xx-1][yy] == 1)update(xx-1,yy,'R'); else update(xx-1,yy,'L'); }else if(cc=='D') { if(xx == N-1) return; int C; if(ans[xx][yy] == 0) C = U[xx][yy]; else if(ans[xx][yy] == 1) C = R[xx][yy]; else C = L[xx][yy]; U[xx+1][yy] = C; if(ans[xx+1][yy] == 0)update(xx+1,yy,'D'); else if(ans[xx+1][yy] == 1)update(xx+1,yy,'L'); else update(xx+1,yy,'R'); }else if(cc=='L') { if(yy == 0) return; int C; if(ans[xx][yy] == 0) C = R[xx][yy]; else if(ans[xx][yy] == 1) C = U[xx][yy]; else C = D[xx][yy]; R[xx][yy-1] = C; if(ans[xx][yy-1] == 0)update(xx,yy-1,'L'); else if(ans[xx][yy-1] == 1)update(xx,yy-1,'D'); else update(xx,yy-1,'U'); }else if(cc=='R') { if(yy == N-1) return; int C; if(ans[xx][yy] == 0) C = L[xx][yy]; else if(ans[xx][yy] == 1) C = D[xx][yy]; else C = U[xx][yy]; L[xx][yy+1] = C; if(ans[xx][yy+1] == 0)update(xx,yy+1,'R'); else if(ans[xx][yy+1] == 1)update(xx,yy+1,'U'); else update(xx,yy+1,'D'); } }; update(x,y,'U'); update(x,y,'D'); update(x,y,'L'); update(x,y,'R'); swap(B[a],B[c]); swap(B[b],B[d]); // rep(i,N){ // rep(j,N) { // cerr << ans[i][j]; // } // cerr << '\n'; // } // cerr << "cost: " << cost << endl; } } for(int i=0;i