結果
問題 | No.460 裏表ちわーわ |
ユーザー | 37zigen |
提出日時 | 2016-12-12 02:28:31 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,032 bytes |
コンパイル時間 | 4,581 ms |
コンパイル使用メモリ | 90,084 KB |
実行使用メモリ | 90,340 KB |
最終ジャッジ日時 | 2024-11-29 12:52:57 |
合計ジャッジ時間 | 16,289 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 107 ms
49,816 KB |
testcase_01 | AC | 119 ms
90,340 KB |
testcase_02 | AC | 148 ms
50,704 KB |
testcase_03 | AC | 111 ms
42,996 KB |
testcase_04 | AC | 118 ms
43,028 KB |
testcase_05 | AC | 126 ms
42,816 KB |
testcase_06 | AC | 126 ms
43,200 KB |
testcase_07 | AC | 116 ms
43,056 KB |
testcase_08 | AC | 145 ms
43,940 KB |
testcase_09 | AC | 137 ms
43,128 KB |
testcase_10 | TLE | - |
testcase_11 | AC | 124 ms
44,264 KB |
testcase_12 | AC | 129 ms
43,424 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | AC | 125 ms
43,328 KB |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | TLE | - |
testcase_20 | AC | 116 ms
41,228 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 126 ms
41,324 KB |
testcase_25 | AC | 117 ms
41,028 KB |
testcase_26 | AC | 114 ms
41,400 KB |
testcase_27 | AC | 104 ms
89,108 KB |
ソースコード
package yukicoder; import java.util.*; import java.util.concurrent.SynchronousQueue; public class Q460 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int M = sc.nextInt(); int N = sc.nextInt(); int[][] a = new int[M][N]; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { a[i][j] = sc.nextInt(); } } int[][] mx = new int[M * N][M * N]; int[] v = new int[M * N]; for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { v[i * N + j] = a[i][j]; } } for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { int nx = j + dx; int ny = i + dy; if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue; mx[i * N + j][ny * N + nx] = 1; } } } } for (int i = 0; i < M * N; ++i) { int j = i; while (j < M * N && mx[j][i] == 0) { ++j; } if (j == M * N) continue; if (i != j) { for (int k = 0; k < N * M; ++k) { int d = mx[i][k]; mx[i][k] = mx[j][k]; mx[j][k] = d; } int d = v[i]; v[i] = v[j]; v[j] = d; } for (int k = 0; k < M * N; ++k) { if (k == i) continue; if (mx[k][i] == 1) { for (int l = 0; l < M * N; ++l) { mx[k][l] = (mx[k][l] + mx[i][l]) % 2; } v[k] = (v[k] + v[i]) % 2; } } } // show(mx, v, M, N); int c = 0; ArrayList<Integer> pending = new ArrayList<>(); for (int i = N * M - 1; i >= 0; --i) { if (v[i] == 1) { if (mx[i][i] != 1) { System.out.println("Impossible"); return; } else { ArrayDeque<Integer> tmp = new ArrayDeque<>(); for (int k = 0; k < N * M; ++k) { if (mx[i][k] == 1) { tmp.add(k); } } if (tmp.size() == 1) { ++c; for (int k = 0; k <= i; ++k) { if (mx[k][i] == 1) { v[k] = v[k] ^ 1; } } } else { pending.addAll(tmp); } } } } int ans = Integer.MAX_VALUE; Collections.sort(pending); for (int i = 0; i < pending.size(); ++i) { while (i + 1 < pending.size() && pending.get(i) == pending.get(i + 1)) { pending.remove(i + 1); ++i; } } outer: for (int i = 0; i < (1 << pending.size()); ++i) { int[] nv = Arrays.copyOf(v, v.length); for (int k = 0; k < pending.size(); ++k) { if ((i & (1 << k)) > 0) { for (int l = 0; l <= pending.get(k); ++l) { if (mx[l][pending.get(k)] == 1) { nv[l] ^= 1; } } } } for (int j = 0; j < N * M; ++j) { if (nv[j] == 1) continue outer; } ans = Math.min(ans, c + Integer.bitCount(i)); } System.out.println(ans); } static void show(int[][] mx, int[] v, int M, int N) { for (int i = 0; i < M * N; ++i) { for (int j = 0; j < M * N; ++j) { System.out.print(mx[i][j] + " "); } System.out.print(v[i]); System.out.println(); } } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }