//ライブラリへのリンク //https://drive.google.com/drive/folders/1QDp2j94gwNBUf6lF2M-FTs9NVAKFpw5R #include using namespace std; using pii=pair; using tii=tuple; using qii=tuple; using ll=long long; using ull=unsigned long long; using ld=long double; constexpr int INF=1e9; constexpr ll INF_ll=1e18; #define rep(i,n) for (int i=0;i<(int)(n);i++) #define replr(i,l,r) for (int i=(int)(l);i<(int)(r);i++) #define all(v) v.begin(),v.end() #define len(v) ((int)v.size()) template inline bool chmin(T &a,T b){ if(a>b){ a=b; return true; } return false; } template inline bool chmax(T &a,T b){ if(a inline bool chmin_ref(T &a,const T &b){ if(a>b){ a=b; return true; } return false; } template inline bool chmax_ref(T &a,const T &b){ if(a(now-start).count(); return ms; } int get_ms_all_program(){ auto now=chrono::steady_clock::now(); int ms=chrono::duration_cast(now-program_start).count(); return ms; } } mt19937 mt; uint32_t rand_int(uint32_t r){ //[0,r) assert(r!=0); return ((uint64_t)mt()*r)>>32; } int rand_int(int l,int r){ //[l,r) assert(l T get_random_element(const vector &v){ assert(!v.empty()); return v[rand_int(len(v))]; } template void add(vector &a,vector b){ for(auto i:b) a.push_back(i); } template struct fenwick_tree{ vector v; int n; fenwick_tree(int x){ n=x; v.assign(n+1,0); } fenwick_tree(vector &a){ n=(int)a.size(); v.assign(n+1,0); for(int i=0;i long long inversion(vector v){ int n=v.size(); fenwick_tree ft(n); long long ret=0; for(int i=0;i SA_State SA(const SA_State &first_state,int limit_ms,double start_temp,double end_temp=0.0){ SA_State state=first_state,best_state=state; int loop_cnt=0,accept_cnt=0; int ms=0; double temp=start_temp; Timer::snap(); constexpr int b=16,sz=1< log_rd; rep(i,sz) log_rd[i]=log(rand_double()); while(true){ if((loop_cnt&1023)==1023){ ms=Timer::get_ms(); if(limit_ms<=ms) break; //temp=start_temp-(start_temp-end_temp)/limit_ms*ms; double t=(double)ms/limit_ms; temp=pow(start_temp,1-t)*pow(end_temp,t); } loop_cnt++; double accept_diff=log_rd[loop_cnt&(sz-1)]*temp; bool accepted=state.modify(accept_diff); if(accepted){ accept_cnt++; if(best_state.score A; array,N*N> G; int one(int i,int j){ return i*N+j; } pii two(int x){ return {x/N,x%N}; } namespace Solver{ struct State{ array mirror; int score=0; State(){ mirror.fill(0); score=calc_score(); } int calc_score(){ vector b(M); rep(t,M){ int d=0,p=0; if(t> n; rep(i,M){ cin >> A[i]; A[i]--; } rep(i,N*N) G[i].fill(-1); rep(i,N){ rep(j,N){ if(j+1