// greedy_xor_v12.cpp // ------------------------------------------------------------------ // • 80 万台 / 90 万台セルを 2 マスずつ残す制約は維持 // • **a 候補を 2 つ選び,現在位置から近い方** を採用 // 1. まず “残数 > reserve” を満たす 80/90 万台セルを // 値が小さい順に最大 2 個収集(high 候補) // 2. 足りなければその他セルから値が小さい順に追加して計 2 個に // 3. その 2 個のうち,Manhattan 距離が短い方を a とする // • stderr には各ステップ詳細 + 最終スコア + 総ステップ数 // • stdout は提出用操作列(1 行 1 文字) // ------------------------------------------------------------------ #include using namespace std; constexpr int MAX_COST12 = 30; constexpr int MAX_COST3 = 45; constexpr int LOW80 = 800000, HIGH80 = 899999; constexpr int LOW90 = 900000, HIGH90 = 999999; struct Plan{ double ratio=-1; int gain=0,cost=0; char type=0; int ar=0,ac=0,b1r=0,b1c=0,b2r=0,b2c=0,b3r=0,b3c=0; }; inline int manh(int r1,int c1,int r2,int c2){ return abs(r1-r2)+abs(c1-c2); } void addMoves(int r1,int c1,int r2,int c2,vector& ops){ if(r2>r1) ops.insert(ops.end(),r2-r1,'D'); else ops.insert(ops.end(),r1-r2,'U'); if(c2>c1) ops.insert(ops.end(),c2-c1,'R'); else ops.insert(ops.end(),c1-c2,'L'); } bool in80(int v){ return LOW80<=v && v<=HIGH80; } bool in90(int v){ return LOW90<=v && v<=HIGH90; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,T; if(!(cin>>N>>T)) return 0; vector> A(N,vector(N)); for(auto& row:A) for(int& x:row) cin>>x; vector> cells; cells.reserve(N*N); for(int i=0;i=2 ? 2 : 0); const int reserve90 = (init90>=2 ? 2 : 0); int remain80 = init80, remain90 = init90; vector> blocked(N,vector(N,0)); int r=0,c=0,s=0; vector ops; int step=1; auto skip_by_reserve=[&](int v){ return (in80(v)&&remain80<=reserve80)||(in90(v)&&remain90<=reserve90); }; while(true){ // ---------- a 候補 2 つを選ぶ ---------- vector> high, rest; for(auto [i,j]:cells){ if(blocked[i][j]) continue; int v=A[i][j]; bool highFlag = (in80(v)&&remain80>reserve80) || (in90(v)&&remain90>reserve90); if(highFlag) high.emplace_back(i,j); else rest.emplace_back(i,j); } auto by_val=[&](auto p1,auto p2){ return A[p1.first][p1.second] < A[p2.first][p2.second]; }; std::sort(high.begin(),high.end(),by_val); std::sort(rest.begin(),rest.end(),by_val); vector> cand; for(auto p:high){ cand.push_back(p); if((int)cand.size()==2) break; } for(auto p:rest){ cand.push_back(p); if((int)cand.size()==2) break; } if(cand.empty()) break; // 全ブロック済み // 近い方を a に int ar=cand[0].first, ac=cand[0].second; if(cand.size()==2){ int d0=manh(r,c, cand[0].first,cand[0].second); int d1=manh(r,c, cand[1].first,cand[1].second); if(d1MAX_COST12||(int)ops.size()+cost>T) continue; int gain=(a_val^(s^A[br][bc]))-a_val; if(gain<=0) continue; double ratio=(double)gain/cost; if(ratio>best.ratio) best={ratio,gain,cost,'1',ar,ac,br,bc}; } // ---------- 2‑copy ---------- for(auto [b1r,b1c]:cells){ if(b1r==ar&&b1c==ac) continue; if(skip_by_reserve(A[b1r][b1c])) continue; for(auto [b2r,b2c]:cells){ if((b2r==ar&&b2c==ac)||(b2r==b1r&&b2c==b1c)) continue; if(skip_by_reserve(A[b2r][b2c])) continue; int cost=manh(r,c,b1r,b1c)+1+manh(b1r,b1c,b2r,b2c)+1+manh(b2r,b2c,ar,ac)+1; if(cost>MAX_COST12||(int)ops.size()+cost>T) continue; int gain=(a_val^(s^A[b1r][b1c]^A[b2r][b2c]))-a_val; if(gain<=0) continue; double ratio=(double)gain/cost; if(ratio>best.ratio) best={ratio,gain,cost,'2',ar,ac,b1r,b1c,b2r,b2c}; } } // ---------- 3‑copy ---------- for(auto [b1r,b1c]:cells){ if(b1r==ar&&b1c==ac) continue; if(skip_by_reserve(A[b1r][b1c])) continue; for(auto [b2r,b2c]:cells){ if((b2r==ar&&b2c==ac)||(b2r==b1r&&b2c==b1c)) continue; if(skip_by_reserve(A[b2r][b2c])) continue; for(auto [b3r,b3c]:cells){ if(b3r==ar&&b3c==ac) continue; if((b3r==b1r&&b3c==b1c)||(b3r==b2r&&b3c==b2c)) continue; if(skip_by_reserve(A[b3r][b3c])) continue; int cost=manh(r,c,b1r,b1c)+1+manh(b1r,b1c,b2r,b2c)+1+ manh(b2r,b2c,b3r,b3c)+1+manh(b3r,b3c,ar,ac)+1; if(cost>MAX_COST3||(int)ops.size()+cost>T) continue; int gain=(a_val^(s^A[b1r][b1c]^A[b2r][b2c]^A[b3r][b3c]))-a_val; if(gain<=0) continue; double ratio=(double)gain/cost; if(ratio>best.ratio) best={ratio,gain,cost,'3',ar,ac,b1r,b1c,b2r,b2c,b3r,b3c}; } } } // ---------- 改善手なし → ブロック ---------- if(best.ratio<0){ blocked[ar][ac]=1; continue; } // ---------- ログ ---------- cerr<<"Step "<