// GPT-6.2 Astra MAX #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; constexpr int N=64, M=4*N; using Matching=array; using Board=array; using Row=array; using Matrix=array; using Clock=chrono::steady_clock; constexpr auto TIME_LIMIT=chrono::milliseconds(1900); Matching A; int cyclic(int i){return i>=M ? i-M : i<0 ? i+M : i;} void rewire(Matching& partners, int a, int b, int c, int d){ partners[a]=b; partners[b]=a; partners[c]=d; partners[d]=c; } void interchange(Matching& p,int a,int b){ if(p[a]!=b) rewire(p,a,p[b],b,p[a]); } void interval_counts(const Matching& p,int start,Row& row){ int count=0, slot=start; for(int length=0;lengthcapacity[k])return false; return true; } void set_row(Matrix& matrix,int gap,const Row& row){ matrix[gap]=row; for(int k=0;k=0;--r)for(int c=N-1;c>=0;--c){ int a=N-1-r+c; interchange(straight,a,a+1); } Matrix need,cap; for(int k=0;k=deadline) return false; int a=N-1-r+c, b=a+1, gap=b; // Remove this crossing from the remaining all-straight diagram. interchange(straight,a,b); Row row; interval_counts(straight,gap,row); set_row(cap,gap,row); if(fits(need[gap],row)){ board[r][c]='2'; continue; } if(wanted[a]!=b){ interchange(wanted,a,b); interval_counts(wanted,gap,row); if(!fits(row,cap[gap]))return false; set_row(need,gap,row); board[r][c]='0'; continue; } // Cap a,b. Split an exterior pair, respecting every tight interval. const int first=cyclic(b+1); array furthest; int end=-1; for(int k=0;k=k && h>end;--h){ int right=cyclic(first+h+1); if(need[l][right]==cap[l][right]){end=h;break;} } furthest[k]=end; } bool found=false; for(int k=0;k coord; array load{}; Change change(int a,int b,int c,int d)const{ int x=coord[a],y=coord[b],u=coord[c],v=coord[d]; int lower=max(min(x,y),min(u,v)); int upper=min(max(x,y),max(u,v)); int other_lower=max(min(x,u),min(y,v)); int other_upper=min(max(x,u),max(y,v)); // Both pairings have the same crossing count except between the middle coordinates. if(lower=other_upper)return {lower+1,upper,-2}; if(other_lower=upper)return {other_lower+1,other_upper,2}; return {1,0,0}; } void apply(Change h){for(int k=h.lo;k<=h.hi;++k)load[k]+=h.delta;} }; int inversion_count(const Matching& b){ int s=0; for(int i=0;ib[j]; return s; } int inversion_swap(Matching& b,int i,int j){ if(i>j)swap(i,j); int x=b[i],y=b[j],lo=min(x,y),hi=max(x,y),cnt=0; for(int k=i+1;k near; array logtab; bool prepare(Clock::time_point deadline){ for(int i=0;i=deadline)return false; iota(near[i].begin(),near[i].end(),0); auto cost=[&](int q){return abs(A[q]-1-i)+abs(A[i]-1-q)+(q==i ? 4*M:0);}; sort(near[i].begin(),near[i].end(),[&](int u,int v){return cost(u) search_matching(Clock::time_point finish, unsigned seed, const Matching& initial, const Proposals& options,int restarts=4,double heat=10) { mt19937 rng(seed); Cuts horiz,vert; Matching mate=initial,b; for(int i=0;iload.fill(0); for(int i=0;icoord[i],cut->coord[mate[i]]); for(int k=l+1;k<=h;++k)++cut->load[k]; } } }; reset_loads(); int score=inversion_count(b),best=score; Matching best_p=mate; auto start=Clock::now(); array mark{}; unsigned it=0; int last_phase=0; double temp=12; constexpr int chains=7; constexpr int chain_percent=55, ksize=20; double millis=chrono::duration(finish-start).count(); if (millis<=0) return {best,best_p}; while(true){ if((it++&255)==0){ double phase=chrono::duration(Clock::now()-start).count()/millis; if(phase>=1)break; double sub=phase*restarts; int cycle=int(sub); if(cycle!=last_phase){ mate=best_p;score=best; for(int i=0;i(chain_percent)) ? 2+rng()%(chains-1) : 1; mark[a]=mark[anchor]=it; int delta=0,n=0; array moves; for(int step=0;step xc,yc; for(int i=0;i=0;--i){ auto [a,b0,c,d]=moves[i]; swap(b[a],b[d]);swap(b[b0],b[c]);rewire(mate,a,b0,c,d); } } assert(inversion_count(b)==score); return {best,best_p}; } Matching trace_board(const Board& board){ Matching mate; for(int p=0;p=0 && r=0 && c=N?N+r:r>=N?3*N-1-c:4*N-1-r; } return mate; } int main(int argc,char** argv){ const auto start=Clock::now(); ios::sync_with_stdio(false); cin.tie(nullptr); const bool report=argc==2; const auto budget=report ? chrono::milliseconds(clamp(atoll(argv[1]),1,TIME_LIMIT.count())) : TIME_LIMIT; const auto until=[&](int percent){return start+budget*percent/100;}; int n; if (!(cin>>n) || n!=N) return 1; for (int& v:A) cin>>v; Board best_board; for(string& row:best_board) row.assign(N,'0'); Matching best_mate=trace_board(best_board), received; for(int i=0;i=best_score || Clock::now()>=deadline)return; Board trial; if(!construct_board(candidate,trial,deadline) || trace_board(trial)!=candidate)return; best_score=score; best_board=move(trial); }; if(options.prepare(until(90))){ auto [warm_score,warm]=search_matching(until(4),23u,initial,options); save(warm_score,warm,until(14)); auto [score,candidate]=search_matching(until(94),2027u,warm,options); save(score,candidate,until(99)); } if(report) cerr<<"inv="<