結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
37zigen
|
| 提出日時 | 2026-11-24 12:23:24 |
| 言語 | Java (openjdk 27.0.1.0.3 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,901 ms / 2,000 ms |
| + 875µs | |
| コード長 | 16,081 bytes |
| 記録 | |
| コンパイル時間 | 2,047 ms |
| コンパイル使用メモリ | 96,220 KB |
| 実行使用メモリ | 49,876 KB |
| スコア | 1,423,577 |
| 最終ジャッジ日時 | 2026-11-24 12:25:14 |
| 合計ジャッジ時間 | 104,250 ms |
|
ジャッジサーバーID (参考情報) |
judge6_0 / judge1_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
public class Main implements java.lang.Runnable {
public static void main(java.lang.String[] args) throws java.io.IOException {
// Runtime runtime = Runtime.getRuntime();
// new Thread(null, new Main(), "MainThreadWithLargeStack", (1024 * 1024) *1024).start();
new Main().run();
// long usedMemory = runtime.totalMemory() - runtime.freeMemory();
// System.err.printf("使用メモリ: %.2f MB%n", usedMemory / 1024.0 / 1024.0);
MyPrintWriter.getInstance().flush();
}
java.util.Random rnd = new java.util.Random();
@java.lang.Override
public void run() {
FastScanner sc = FastScanner.getInstance();
MyPrintWriter pw = MyPrintWriter.getInstance();
int N = sc.nextInt();
int[] A = sc.nextInts(4 * N);
long startTime = java.lang.System.nanoTime();
ArrayUtils.decrementAll(A);
char[][] S = new char[N][N];
ArrayUtils.fill(S, '0');
// MinimumCostFlow mf=new MinimumCostFlow(4*N+2);
// for (int i = 0; i < N; i++) {
// mf.addEdge(i, 3*N-1-i, cost(i, A[i], 3*N-1-i, A[3*N-1-i]), 1);
// }
// for (int i = 0; i < N; i++) {
// for (int j = N; j < 4*N; j++) {
// if(2*N <= j && j < 3*N) continue;
// mf.addEdge(i, j, cost(i, A[i], j, A[j]), 1);
// }
// }
// int source=4*N;
// int sink=source+1;
// for (int i = 0; i < N; i++) {
// mf.addFixedFlowEdge(source, i, 0, 1);
// }
// for (int i = N; i < 4*N; i++) {
// mf.addEdge(i, sink, 0, 1);
// }
// mf.minCostFlowAtMost(source, sink, N);
// for (int i = 0; i < N; i++) {
// for(var e : mf.g[i]) {
// if(e.flow > 0) {
// if (N <= e.to && e.to < 2*N) {
// S[e.to-N][i]='2';
// }
// if (3*N <= e.to && e.to < 4*N) {
// S[N-1-(e.to-3*N)][i]='1';
// }
// }
// }
// }
// pw.println(S);
long TL = (1900L * 1000) * 1000;
long curScore = eval(A, S);
for (int T = 0; T < 1000000; T++) {
if ((T % 100) == 0) {
// System.err.println("iteration"+T);
long dt = java.lang.System.nanoTime() - startTime;
if (dt >= TL) {
break;
}
}
int i = rnd.nextInt(N);
int j = rnd.nextInt(N);
int v = rnd.nextInt(3);
if ((S[i][j] - '0') == v) {
continue;
}
char pre = S[i][j];
S[i][j] = ((char) ('0' + v));
long score = eval(A, S);
if (score > curScore) {
S[i][j] = pre;
} else {
// System.err.println("update at T="+T);
curScore = score;
}
}
pw.println(S);
}
long eval(int[] A, char[][] S) {
int N = S.length;
int[] B = new int[4 * N];
int[] di = new int[]{ 1, 0, -1, 0 };
int[] dj = new int[]{ 0, -1, 0, 1 };
for (int t = 0; t < A.length; t++) {
int dir = t / N;// ↓、←、↑、→
int i;
int j;
if (t < N) {
i = 0;
j = t;
} else if (t < (2 * N)) {
i = t % N;
j = N - 1;
} else if (t < (3 * N)) {
i = N - 1;
j = (N - 1) - (t % N);
} else {
i = (N - 1) - (t % N);
j = 0;
}
while (true) {
if (S[i][j] == '1') {
if (dir == 0) {
dir = 1;
} else if (dir == 1) {
dir = 0;
} else if (dir == 2) {
dir = 3;
} else if (dir == 3) {
dir = 2;
}
} else if (S[i][j] == '2') {
// ↓、←、↑、→
if (dir == 0) {
dir = 3;
} else if (dir == 3) {
dir = 0;
} else if (dir == 2) {
dir = 1;
} else if (dir == 1) {
dir = 2;
}
}
i += di[dir];
j += dj[dir];
if (!((((0 <= i) && (i < N)) && (0 <= j)) && (j < N))) {
if (i == (-1)) {
B[j] = A[t];
} else if (i == N) {
B[((3 * N) - 1) - j] = A[t];
} else if (j == (-1)) {
B[((4 * N) - 1) - i] = A[t];
} else if (j == N) {
B[N + i] = A[t];
}
break;
}
}
}
// int ret = 0;
// for (int i = 0; i < N; i++) {
// ret += Math.abs(i - B[i]);
// }
// return ret;
return Permutation.inversion(B);
}
}
class ArrayUtils {
public static void decrementAll(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i]--;
}
}
public static void fill(char[][] a, char v) {
ArrayUtils2D.fill(a, v);
}
}
/**
* 2次元配列操作のユーティリティクラス。
*/
class ArrayUtils2D {
public static void fill(char[][] a, char v) {
for (int i = 0; i < a.length; ++i) {
for (int j = 0; j < a[i].length; ++j) {
a[i][j] = v;
}
}
}
}
class FastScanner {
private static FastScanner instance = null;
private final java.io.InputStream in = java.lang.System.in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0;
private int buflen = 0;
private FastScanner() {
}
public static FastScanner getInstance() {
if (FastScanner.instance == null) {
FastScanner.instance = new FastScanner();
}
return FastScanner.instance;
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
return buflen > 0;
}
private int readByte() {
if (hasNextByte()) {
return buffer[ptr++];
} else {
return -1;
}
}
private boolean isPrintableChar(int c) {
return (33 <= c) && (c <= 126);
}
public boolean hasNext() {
while (hasNextByte() && (!isPrintableChar(buffer[ptr]))) {
ptr++;
}
return hasNextByte();
}
public long nextLong() {
if (!hasNext()) {
throw new java.util.NoSuchElementException();
}
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
while ((b >= '0') && (b <= '9')) {
// n = n * 10 + (b - '0');
n = ((n << 1) + (n << 3)) + (b - '0');
b = readByte();
}
return minus ? -n : n;
}
public int nextInt() {
return ((int) (nextLong()));
}
public int[] nextInts(int n) {
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = nextInt();
}
return a;
}
}
class MyPrintWriter extends java.io.PrintWriter {
private static MyPrintWriter instance = null;
private MyPrintWriter() {
super(java.lang.System.out);
}
public static MyPrintWriter getInstance() {
if (MyPrintWriter.instance == null) {
MyPrintWriter.instance = new MyPrintWriter();
}
return MyPrintWriter.instance;
}
public MyPrintWriter(java.io.PrintStream out) {
super(out);
}
public void println(char[][] a) {
for (int i = 0; i < a.length; i++) {
println(a[i]);
}
}
}
class Permutation {
public static long inversion(int[] a) {
int n = a.length;
PopcountBIT tree = new PopcountBIT(n);
long ans = 0;
for (int i = 0; i < a.length; ++i) {
ans += i - tree.sum(a[i]);
tree.set(i, true);
}
return ans;
}
}
/**
* 1 列に対する Binary Indexed Tree (popcount 版)。
*
* 64 要素を 1 ワード(long)にまとめ、ワード内は Long.bitCount で O(1) に処理し、
* ワード単位の個数だけを Fenwick tree で管理する。
* よって Fenwick の大きさは n/64 となり、各操作は O(log n - log 64)。
* メモリも n/64 個の long + n/64 個の int で済む。
*/
class PopcountBIT {
private final int n;
private final int words;// ワード数
private final long[] bits;// 0-indexed のビット列
private final int[] tree;// Fenwick (1-indexed): ワード j は位置 j+1
private int total;
// 全体の 1 の個数
/**
* 全て 0 で初期化。
*/
public PopcountBIT(int n) {
this.n = n;
this.words = (n >>> 6) + 1;// 番兵として 1 ワード余分に確保 (sum(n) の境界処理を不要にする)
this.bits = new long[words];
this.tree = new int[words + 1];
}
/**
* boolean 配列から O(n/64) で構築。
*/
public PopcountBIT(boolean[] a) {
this(a.length);
for (int i = 0; i < n; i++) {
if (a[i]) {
bits[i >>> 6] |= 1L << (i & 63);
}
}
for (int j = 0; j < words; j++) {
int c = java.lang.Long.bitCount(bits[j]);
total += c;
tree[j + 1] += c;
int p = (j + 1) + ((j + 1) & (-(j + 1)));
if (p <= words) {
tree[p] += tree[j + 1];
}
}
}
/**
* a[i] を返す。O(1)
*/
public boolean get(int i) {
return ((bits[i >>> 6] >>> (i & 63)) & 1L) != 0;
}
/**
* a[i] = v。変化があった場合のみ Fenwick を更新。O(log n - log 64)
*/
public void set(int i, boolean v) {
if (get(i) == v) {
return;
}
flip(i);
}
/**
* a[i] を反転。O(log n - log 64)
*/
public void flip(int i) {
int w = i >>> 6;
long mask = 1L << (i & 63);
int d = ((bits[w] & mask) == 0) ? 1 : -1;
bits[w] ^= mask;
total += d;
for (int x = w + 1; x <= words; x += x & (-x)) {
tree[x] += d;
}
}
/**
* [0, r) の 1 の個数。O(log n - log 64)
*/
public int sum(int r) {
int w = r >>> 6;
int s = java.lang.Long.bitCount(bits[w] & ((1L << (r & 63)) - 1));// 端数ワード (r&63==0 なら 0)
for (int x = w; x > 0; x -= x & (-x)) {
s += tree[x];
}// ワード [0, w)
return s;
}
}
// --- Original Code ---
//library:https://github.com/Maruoka842/library_public
//
// import java.io.IOException;
// import java.util.Arrays;
// import java.util.Random;
//
// import library.tools.FastScanner;
// import library.tools.JavaToCpp;
// import library.tools.MergeFiles;
// import library.tools.MyPrintWriter;
// import library.util.ArrayUtils;
// import library.util.collections.IntArrayList;
// import library.util.graph.flow.MinimumCostFlow;
// import library.util.seq.Permutation;
//
// public class Main implements Runnable {
//
// public static void main(String[] args) throws IOException {
// // Runtime runtime = Runtime.getRuntime();
// // new Thread(null, new Main(), "MainThreadWithLargeStack", (1024 * 1024) *1024).start();
// new Main().run();
// // long usedMemory = runtime.totalMemory() - runtime.freeMemory();
// // System.err.printf("使用メモリ: %.2f MB%n", usedMemory / 1024.0 / 1024.0);
// MyPrintWriter.getInstance().flush();
// MergeFiles.export();
// JavaToCpp.export();
// }
//
// Random rnd=new Random();
//
// @java.lang.Override
// public void run() {
// FastScanner sc = FastScanner.getInstance();
// MyPrintWriter pw = MyPrintWriter.getInstance();
// int N=sc.nextInt();
// int[]A=sc.nextInts(4*N);
// long startTime=System.nanoTime();
// ArrayUtils.decrementAll(A);
// char[][] S = new char[N][N];
// ArrayUtils.fill(S, '0');
// // MinimumCostFlow mf=new MinimumCostFlow(4*N+2);
// // for (int i = 0; i < N; i++) {
// // mf.addEdge(i, 3*N-1-i, cost(i, A[i], 3*N-1-i, A[3*N-1-i]), 1);
// // }
// // for (int i = 0; i < N; i++) {
// // for (int j = N; j < 4*N; j++) {
// // if(2*N <= j && j < 3*N) continue;
// // mf.addEdge(i, j, cost(i, A[i], j, A[j]), 1);
// // }
// // }
// // int source=4*N;
// // int sink=source+1;
// // for (int i = 0; i < N; i++) {
// // mf.addFixedFlowEdge(source, i, 0, 1);
// // }
// // for (int i = N; i < 4*N; i++) {
// // mf.addEdge(i, sink, 0, 1);
// // }
// // mf.minCostFlowAtMost(source, sink, N);
// // for (int i = 0; i < N; i++) {
// // for(var e : mf.g[i]) {
// // if(e.flow > 0) {
// // if (N <= e.to && e.to < 2*N) {
// // S[e.to-N][i]='2';
// // }
// // if (3*N <= e.to && e.to < 4*N) {
// // S[N-1-(e.to-3*N)][i]='1';
// // }
// // }
// // }
// // }
// // pw.println(S);
// long TL=1900L*1000*1000;
// long curScore = eval(A, S);
// for (int T = 0; T < 1000000; T++) {
// if(T%100==0) {
// // System.err.println("iteration"+T);
// long dt=System.nanoTime()-startTime;
// if(dt>=TL)break;
// }
// int i = rnd.nextInt(N);
// int j = rnd.nextInt(N);
// int v = rnd.nextInt(3);
// if(S[i][j] - '0' == v) continue;
// char pre = S[i][j];
// S[i][j] = (char)('0' + v);
// long score = eval(A, S);
// if(score > curScore) {
// S[i][j] = pre;
// } else {
// // System.err.println("update at T="+T);
// curScore = score;
// }
// }
// pw.println(S);
//
// }
//
// int cost(int i0, int v0, int i1, int v1) {
// int d0=Math.abs(i0 - v1);
// int d1=Math.abs(i1 - v0);
// return d0+d1;
// }
//
// long eval(int[]A, char[][]S) {
// int N=S.length;
// int[]B=new int[4 * N];
// int[]di=new int[] {1, 0, -1, 0};
// int[]dj=new int[] {0, -1, 0, 1};
// for (int t = 0; t < A.length; t++) {
// int dir=t/N;//↓、←、↑、→
// int i, j;
// if (t < N) {
// i = 0;
// j = t;
// } else if (t < 2*N) {
// i = t % N;
// j = N - 1;
// } else if (t < 3*N) {
// i = N - 1;
// j = N - 1 - t % N;
// } else {
// i = N - 1 - t % N;
// j = 0;
// }
// while(true) {
// if (S[i][j] == '1') {
// if (dir == 0) dir = 1;
// else if (dir == 1) dir = 0;
// else if (dir == 2) dir = 3;
// else if (dir == 3) dir = 2;
// } else if (S[i][j] == '2') {
// ////↓、←、↑、→
// if (dir == 0) dir = 3;
// else if (dir == 3) dir = 0;
// else if (dir == 2) dir = 1;
// else if (dir == 1) dir = 2;
// }
// i += di[dir];
// j += dj[dir];
// if (!(0 <= i && i < N && 0 <= j && j < N)) {
// if (i == -1) {
// B[j] = A[t];
// } else if (i == N) {
// B[3*N-1-j] = A[t];
// } else if (j == -1) {
// B[4*N-1-i] = A[t];
// } else if (j == N) {
// B[N+i] = A[t];
// }
// break;
// }
// }
// }
// // int ret = 0;
// // for (int i = 0; i < N; i++) {
// // ret += Math.abs(i - B[i]);
// // }
// // return ret;
// return Permutation.inversion(B);
// }
//
//
// void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
// }
//
//
37zigen