結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-24 03:18:32 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,791 ms / 2,000 ms |
| + 144µs | |
| コード長 | 11,967 bytes |
| 記録 | |
| コンパイル時間 | 1,979 ms |
| コンパイル使用メモリ | 245,020 KB |
| 実行使用メモリ | 9,892 KB |
| スコア | 1,560,023 |
| 最終ジャッジ日時 | 2026-11-24 03:20:09 |
| 合計ジャッジ時間 | 96,801 ms |
|
ジャッジサーバーID (参考情報) |
judge5_0 / judge2_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
// GPT-6.2 Astra MAX
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <utility>
#include <iostream>
#include <numeric>
#include <random>
#include <string>
using namespace std;
constexpr int N=64, M=4*N;
using Matching=array<int,M>;
using Board=array<string,N>;
using Row=array<unsigned char,M>;
using Matrix=array<Row,M>;
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;length<M;++length){
row[slot]=count;
count += (cyclic(p[slot]-start)<length) ? -1 : 1;
slot=cyclic(slot+1);
}
}
bool fits(const Row& need,const Row& capacity){
for(int k=0;k<M;++k) if(need[k]>capacity[k])return false;
return true;
}
void set_row(Matrix& matrix,int gap,const Row& row){
matrix[gap]=row;
for(int k=0;k<M;++k)matrix[k][gap]=row[k];
}
// Sweep the crossings of the initially straight 64 horizontal and 64 vertical wires.
// An interval needs as many exits as it has unmatched terminal pairs.
bool construct_board(const Matching& mate, Board& board, Clock::time_point deadline){
// Frontier indices start at the bottom of the left side and run clockwise.
Matching wanted, straight;
for(int k=0;k<M;++k){
wanted[k]=cyclic(mate[cyclic(3*N+k)]-3*N);
straight[k]=M-1-k;
}
for(int r=N-1;r>=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<M;++k){
interval_counts(wanted,k,need[k]);
interval_counts(straight,k,cap[k]);
if(!fits(need[k],cap[k]))return false;
}
for(int r=0;r<N;++r){
board[r].assign(N,'0');
for(int c=0;c<N;++c){
if ((c & 7)==0 && Clock::now()>=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<int,M> furthest;
int end=-1;
for(int k=0;k<M-2;++k){
const int l=cyclic(first+k);
for(int h=M-3;h>=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<M-2 && !found;++k){
int x=cyclic(first+k), y=wanted[x], h=cyclic(y-first);
if(h<=k || h<=furthest[k])continue;
for(int rev=0;rev<2;++rev){
Matching trial=wanted;
rewire(trial,a,x,b,y);
interval_counts(trial,gap,row);
if(fits(row,cap[gap])){
wanted=trial;
// Away from gap, only intervals containing both exterior
// endpoints (and their complements) gain two crossings.
for(int u=0;u<=k;++u){
int left=cyclic(first+u);
for(int v=h+1;v<=M-2;++v){
int right=cyclic(first+v);
need[left][right]+=2;
need[right][left]+=2;
}
}
set_row(need,gap,row);
found=true;
break;
}
swap(x,y);
}
}
if(!found)return false;
board[r][c]='1';
}
}
return wanted==straight;
}
struct Change{int lo,hi,delta;};
struct Cuts{
array<int,M> coord;
array<int,N> 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<upper && other_lower>=other_upper)return {lower+1,upper,-2};
if(other_lower<other_upper && 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;i<M;++i)for(int j=i+1;j<M;++j)s+=b[i]>b[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<j;++k)cnt+=(lo<b[k])&(b[k]<hi);
swap(b[i],b[j]);
return (x<y?1:-1)*(1+2*cnt);
}
struct Move {int a,b,c,d;};
struct Proposals {
array<Matching,M> near;
array<double,4096> logtab;
bool prepare(Clock::time_point deadline){
for(int i=0;i<M;++i){
if((i&7)==0 && Clock::now()>=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)<cost(v);});
}
for(int k=0;k<4096;++k)logtab[k]=-log((k+0.5)/4096.0);
return true;
}
};
void set_coordinates(Cuts& horiz,Cuts& vert){
for(int i=0;i<M;++i){
int side=i/N,pos=i%N;
horiz.coord[i]=side==0?pos:side==1?N-1:side==2?N-1-pos:0;
vert.coord[i]=side==0?0:side==1?pos:side==2?N-1:N-1-pos;
}
}
// Propose an alternating chain of pair exchanges and test its final cut loads.
pair<int,Matching> 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;i<M;++i)b[i]=A[mate[i]];
set_coordinates(horiz,vert);
auto reset_loads=[&](){
for(Cuts* cut:{&horiz,&vert}){
cut->load.fill(0);
for(int i=0;i<M;++i)if(i<mate[i]){
auto [l,h]=minmax(cut->coord[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<unsigned,M> 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<double,milli>(finish-start).count();
if (millis<=0) return {best,best_p};
while(true){
if((it++&255)==0){
double phase=chrono::duration<double,milli>(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<M;++i)b[i]=A[mate[i]];
reset_loads();
last_phase=cycle;
}
temp=heat*pow(.18/heat,sub-cycle);
}
int a=rng()%M, anchor=mate[a];
int length=(rng()%100<static_cast<unsigned>(chain_percent)) ? 2+rng()%(chains-1) : 1;
mark[a]=mark[anchor]=it;
int delta=0,n=0;
array<Move,chains> moves;
for(int step=0;step<length;++step){
int c;
if(rng()%16==0)c=rng()%M;
else c=options.near[a][rng()%ksize];
int d=mate[c];
if(mark[c]==it || mark[d]==it)break;
moves[n++]={a,anchor,c,d};
delta+=inversion_swap(b,a,d);
delta+=inversion_swap(b,anchor,c);
rewire(mate,a,c,anchor,d);
mark[c]=mark[d]=it;
a=d;
}
if(n==0)continue;
bool accepted=delta<=temp*options.logtab[rng()%4096];
if(accepted){
array<Change,chains> xc,yc;
for(int i=0;i<n;++i){
auto [a,b0,c,d]=moves[i];
xc[i]=horiz.change(a,b0,c,d);yc[i]=vert.change(a,b0,c,d);
horiz.apply(xc[i]);vert.apply(yc[i]);
}
if(*max_element(horiz.load.begin(),horiz.load.end())<=N &&
*max_element(vert.load.begin(),vert.load.end())<=N){
score+=delta;
if(score<best){best=score;best_p=mate;}
continue;
}
for(int i=0;i<n;++i){
xc[i].delta=-xc[i].delta;yc[i].delta=-yc[i].delta;
horiz.apply(xc[i]);vert.apply(yc[i]);
}
}
for(int i=n-1;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<M;++p){
int r,c,dr,dc;
if(p<N){r=0;c=p;dr=1;dc=0;}else if(p<2*N){r=p-N;c=N-1;dr=0;dc=-1;}
else if(p<3*N){r=N-1;c=3*N-1-p;dr=-1;dc=0;}else{r=4*N-1-p;c=0;dr=0;dc=1;}
while(r>=0 && r<N && c>=0 && c<N){
char cell=board[r][c];
if(cell!='0'){
swap(dr,dc);if(cell=='1'){dr=-dr;dc=-dc;}
}
r+=dr;c+=dc;
}
mate[p]=r<0?c: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<long long>(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<M;++i) received[i]=A[best_mate[i]];
int best_score=inversion_count(received);
Matching initial;
for(int i=0;i<M;++i) initial[i]=i^1;
Proposals options;
auto save=[&](int score,const Matching& candidate,Clock::time_point deadline){
if(score>=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="<<best_score<<" points="<<M*(M-1)/2-best_score<<'\n';
for(const string& row:best_board) cout<<row<<'\n';
}