結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー ks2m
提出日時 2026-11-23 21:45:06
言語 Java
(openjdk 27.0.1.0.3 + ACL)
コンパイル:
javac -J-Duser.language=en -encoding UTF8 -cp /opt/aclib/ac_library.jar _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true -cp .:/opt/aclib/ac_library.jar _class_
結果
AC  
実行時間 1,803 ms / 2,000 ms
+ 546µs
コード長 5,243 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,031 ms
コンパイル使用メモリ 113,612 KB
実行使用メモリ 51,992 KB
スコア 1,304,559
最終ジャッジ日時 2026-11-23 21:46:47
合計ジャッジ時間 100,354 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge5_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;

public class Main {
	static boolean batch = false;
	static boolean readFile = false;
	static boolean outFile = false;
	static int caseNum = 0;

	static double startTemp = 10;
	static double endTemp = 1;
	static long limitTime = 1800;

	static int[] dx = {1, 0, -1, 0};
	static int[] dy = {0, 1, 0, -1};

	static int N, M, NN;
	static int[] a;

	static PrintWriter pw;
	static int okCnt, ngCnt;

	public static void main(String[] args) throws Exception {
		if (batch) {
			readFile = true;
		}
		if (readFile) {
			if (batch) {
				long tl = 2000;
				long tw = tl - 200;
				long total = 0;
				int bidx = 0;
				long best = 0;
				int widx = 0;
				long worst = 1000000000000000000L;
				int re = 0;
				int tle = 0;
				for (int z = 0; z < 100; z++) {
					try {
						long st = System.currentTimeMillis();
						long score = solve(z);
						long time = System.currentTimeMillis() - st;
						if (time > tw) {
							System.out.println(score + "\t" + time + "ms");
							if (time > tl) {
								tle++;
							}
						} else {
							System.out.println(score);
						}
						total += score;
						if (score > best) {
							best = score;
							bidx = z;
						}
						if (score < worst) {
							worst = score;
							widx = z;
						}
					} catch (Exception e) {
						System.out.println(z + ":\t" + e.getMessage());
						re++;
					}
				}
				System.out.println("total: " + total);
				System.out.println("best: " + bidx + ": " + best);
				System.out.println("worst: " + widx + ": " + worst);
				System.out.println("RE: " + re);
				System.out.println("TLE: " + tle);
			} else {
				long score = solve(caseNum);
				System.out.println(okCnt + "\t" + ngCnt);
				System.out.println(score);
			}

		} else {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			init(br);
			br.close();
			solve();
		}
	}

	static long solve(int z) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(z);
		while (sb.length() < 4) {
			sb.insert(0, '0');
		}
		File file = new File("G:\\yuki\\5026\\in\\" + sb.toString() + ".txt");
		BufferedReader br = new BufferedReader(new FileReader(file));
		init(br);
		br.close();
		if (outFile) {
			File file2 = new File("G:\\yuki\\5026\\out\\" + sb.toString() + ".txt");
			pw = new PrintWriter(file2);
		}
		long res = solve();
		if (outFile) {
			pw.close();
		}
		return res;
	}

	static void init(BufferedReader br) throws Exception {
		N = Integer.parseInt(br.readLine());
		M = N * 4;
		NN = N * N;

		String[] sa = br.readLine().split(" ");
		a = new int[M];
		for (int i = 0; i < M; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
	}

	static long solve() {
		char[][] s = new char[N][N];
		for (int i = 0; i < N; i++) {
			Arrays.fill(s[i], '0');
		}

		long startTime = System.currentTimeMillis();
		long preScore = simulate(s);
		while (true) {
			long time = System.currentTimeMillis() - startTime;
			if (time > limitTime) {
				break;
			}
			double ratio = (double) time / limitTime;
			double temp = startTemp * (1 - ratio) + endTemp * ratio;

			int p = (int) (Math.random() * NN);
			int x = p / N;
			int y = p % N;
			char pc = s[x][y];
			int nc = pc - '0';
			nc++;
			if (nc == 3) {
				nc = 0;
			}
			s[x][y] = (char) ('0' + nc);
			long newScore = simulate(s);

			double prob = Math.exp((preScore - newScore) / temp);
			if (Math.random() < prob) {
				// 更新
				preScore = newScore;
				okCnt++;
			} else {
				// 戻す
				s[x][y] = pc;
				ngCnt++;
			}
		}

		if (outFile) {
			output(pw, s);
		} else if (!batch) {
			output(new PrintWriter(System.out), s);
		}

		long score = M * (M - 1) / 2 - preScore;
		return score;
	}

	static long simulate(char[][] s) {
		int[] b = new int[M];
		for (int i = 0; i < M; i++) {
			int x = 0;
			int y = 0;
			int d = 0;
			if (i < N) {
				x = 0;
				y = i;
				d = 0;
			} else if (i < N * 2) {
				x = i - N;
				y = N - 1;
				d = 3;
			} else if (i < N * 3) {
				x = N - 1;
				y = N * 3 - 1 - i;
				d = 2;
			} else {
				x = N * 4 - 1 - i;
				y = 0;
				d = 1;
			}
			while (0 <= x && x < N && 0 <= y && y < N) {
				if (s[x][y] == '1') {
					d = 3 - d;
				} else if (s[x][y] == '2') {
					d = d ^ 1;
				}
				x += dx[d];
				y += dy[d];
			}
			if (x < 0) {
				b[y] = a[i];
			} else if (y >= N) {
				b[N + x] = a[i];
			} else if (x >= N) {
				b[N * 3 - 1 - y] = a[i];
			} else {
				b[N * 4 - 1 - x] = a[i];
			}
		}
		return tentousuu(b);
	}

	static long tentousuu(int[] b) {
		BIT bit = new BIT(M);
		long ret = 0;
		for (int i = 0; i < M; i++) {
			ret += i - bit.sum(b[i]);
			bit.add(b[i], 1);
		}
		return ret;
	}

	static class BIT {
		int n;
		long[] arr;

		public BIT(int n) {
			this.n = n;
			arr = new long[n + 1];
		}

		void add(int idx, long val) {
			for (int i = idx; i <= n; i += i & -i) {
				arr[i] += val;
			}
		}

		long sum(int idx) {
			long sum = 0;
			for (int i = idx; i > 0; i -= i & -i) {
				sum += arr[i];
			}
			return sum;
		}
	}

	static void output(PrintWriter pw, char[][] s) {
		for (int i = 0; i < N; i++) {
			pw.println(s[i]);
		}
		pw.flush();
	}
}
0