結果
問題 | No.421 しろくろチョコレート |
ユーザー | 37zigen |
提出日時 | 2016-09-09 13:13:20 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 208 ms / 2,000 ms |
コード長 | 6,325 bytes |
コンパイル時間 | 4,920 ms |
コンパイル使用メモリ | 84,004 KB |
実行使用メモリ | 56,224 KB |
最終ジャッジ日時 | 2024-09-23 07:04:53 |
合計ジャッジ時間 | 13,723 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
49,916 KB |
testcase_01 | AC | 118 ms
53,912 KB |
testcase_02 | AC | 126 ms
53,620 KB |
testcase_03 | AC | 60 ms
50,288 KB |
testcase_04 | AC | 99 ms
53,968 KB |
testcase_05 | AC | 66 ms
50,736 KB |
testcase_06 | AC | 58 ms
49,912 KB |
testcase_07 | AC | 105 ms
53,132 KB |
testcase_08 | AC | 127 ms
53,596 KB |
testcase_09 | AC | 61 ms
49,800 KB |
testcase_10 | AC | 62 ms
50,340 KB |
testcase_11 | AC | 112 ms
54,004 KB |
testcase_12 | AC | 57 ms
49,956 KB |
testcase_13 | AC | 55 ms
50,388 KB |
testcase_14 | AC | 54 ms
49,768 KB |
testcase_15 | AC | 54 ms
49,764 KB |
testcase_16 | AC | 148 ms
54,472 KB |
testcase_17 | AC | 149 ms
54,776 KB |
testcase_18 | AC | 149 ms
53,880 KB |
testcase_19 | AC | 55 ms
49,924 KB |
testcase_20 | AC | 132 ms
54,140 KB |
testcase_21 | AC | 131 ms
53,800 KB |
testcase_22 | AC | 110 ms
53,500 KB |
testcase_23 | AC | 54 ms
49,664 KB |
testcase_24 | AC | 55 ms
49,500 KB |
testcase_25 | AC | 55 ms
49,560 KB |
testcase_26 | AC | 56 ms
50,296 KB |
testcase_27 | AC | 56 ms
49,552 KB |
testcase_28 | AC | 141 ms
54,232 KB |
testcase_29 | AC | 148 ms
54,828 KB |
testcase_30 | AC | 136 ms
54,088 KB |
testcase_31 | AC | 180 ms
56,224 KB |
testcase_32 | AC | 133 ms
53,864 KB |
testcase_33 | AC | 144 ms
54,420 KB |
testcase_34 | AC | 127 ms
53,860 KB |
testcase_35 | AC | 132 ms
53,452 KB |
testcase_36 | AC | 106 ms
54,152 KB |
testcase_37 | AC | 159 ms
55,296 KB |
testcase_38 | AC | 192 ms
56,068 KB |
testcase_39 | AC | 125 ms
53,972 KB |
testcase_40 | AC | 128 ms
55,400 KB |
testcase_41 | AC | 78 ms
50,932 KB |
testcase_42 | AC | 68 ms
50,480 KB |
testcase_43 | AC | 118 ms
54,144 KB |
testcase_44 | AC | 158 ms
55,772 KB |
testcase_45 | AC | 94 ms
53,344 KB |
testcase_46 | AC | 85 ms
52,932 KB |
testcase_47 | AC | 129 ms
54,000 KB |
testcase_48 | AC | 180 ms
56,212 KB |
testcase_49 | AC | 126 ms
54,048 KB |
testcase_50 | AC | 116 ms
53,764 KB |
testcase_51 | AC | 181 ms
55,844 KB |
testcase_52 | AC | 107 ms
53,404 KB |
testcase_53 | AC | 81 ms
53,244 KB |
testcase_54 | AC | 126 ms
53,836 KB |
testcase_55 | AC | 59 ms
49,744 KB |
testcase_56 | AC | 60 ms
50,200 KB |
testcase_57 | AC | 80 ms
50,344 KB |
testcase_58 | AC | 132 ms
53,980 KB |
testcase_59 | AC | 88 ms
50,952 KB |
testcase_60 | AC | 169 ms
56,024 KB |
testcase_61 | AC | 208 ms
55,844 KB |
testcase_62 | AC | 56 ms
49,712 KB |
testcase_63 | AC | 55 ms
49,864 KB |
testcase_64 | AC | 127 ms
53,732 KB |
ソースコード
package No400番台; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; public class Q421_0 { int[] dx = { 1, -1, 0, 0 }; int[] dy = { 0, 0, 1, -1 }; int h, w; char[][] choco; int[][] comp; HashMap<Integer, Pair> map = new HashMap<>(); @SuppressWarnings("unchecked") void solver() { h = ni(); w = ni(); assert 1 <= h && h <= 100 && 1 <= w && w <= 100; choco = new char[h][w]; for (int i = 0; i < h; i++) { choco[i] = ns().toCharArray(); assert choco[i].length == w; } comp = new int[h][w]; for (int i = 0; i < h; i++) { Arrays.fill(comp[i], -1); } int color = 0; int ans = 0; int odd = 0, even = 0; ArrayList<Integer>[] g = new ArrayList[h * w]; for (int i = 0; i < h * w; i++) { g[i] = new ArrayList<>(); } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { assert (x + y) % 2 == 0 ? choco[y][x] != 'b' : choco[y][x] != 'w'; if (on_filed(x, y) && comp[y][x] == -1) { ArrayDeque<Pair> que = new ArrayDeque<>(); que.add(new Pair(x, y)); while (!que.isEmpty()) { Pair p = que.poll(); if (comp[p.y][p.x] != -1) continue; comp[p.y][p.x] = color; if ((p.x + p.y) % 2 == 0) even++; else odd++; for (int i = 0; i < 4; i++) { int nx = p.x + dx[i]; int ny = p.y + dy[i]; if (on_filed(nx, ny) && comp[ny][nx] == -1) { int a = id(p.x, p.y); int b = id(nx, ny); map.put(a, new Pair(p.x, p.y)); map.put(b, new Pair(nx, ny)); g[a].add(b); g[b].add(a); que.add(new Pair(nx, ny)); } } } color++; } } } int the_number_of_matchings = bipartiteMatching(g, (h * w + 1) / 2); ans += the_number_of_matchings * 100; even -= the_number_of_matchings; odd -= the_number_of_matchings; // show_comp(); int common = Math.min(even, odd); ans += common * 10; even -= common; odd -= common; ans += even + odd; out.println(ans); } int id(int x, int y) { if ((y + x) % 2 == 0) return (y * w + x) / 2; else return (y * w + x) / 2 + (h * w - 1) / 2 + 1; } boolean on_filed(int x, int y) { if (0 <= x && x < w && 0 <= y && y < h && choco[y][x] != '.') { return true; } else return false; } class Pair { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } } boolean augment(ArrayList<Integer>[] g, int u, Integer[] matchTo, Boolean[] visited) { if (u < 0) return true; for (int v : g[u]) { if (!visited[v]) { visited[v] = true; if (augment(g, matchTo[v], matchTo, visited)) { matchTo[u] = v; matchTo[v] = u; return true; } } } return false; } int bipartiteMatching(ArrayList<Integer>[] g, int L) { final int n = g.length; Integer[] matchTo = new Integer[n]; Arrays.fill(matchTo, -1); matching = new ArrayList<>(); int match = 0; for (int i = 0; i < L; i++) { Boolean[] visited = new Boolean[n]; Arrays.fill(visited, false); if (augment(g, i, matchTo, visited)) match++; } for (int i = 0; i < L; i++) { if (matchTo[i] > 0) matching.add(new Edge(i, matchTo[i])); } return match; } ArrayList<Edge> matching; class Edge { int a; int b; Edge(int a, int b) { this.a = a; this.b = b; } } void show_comp() { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { System.out.print((comp[i][j] == -1 ? "#" : comp[i][j]) + " "); } System.out.println(); } } static InputStream is; static PrintWriter out; static String INPUT = ""; public static void main(String[] args) throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); new Q421_0().solver(); out.flush(); } static long nl() { try { long num = 0; boolean minus = false; while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-')) ; if (num == '-') { num = 0; minus = true; } else { num -= '0'; } while (true) { int b = is.read(); if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } } } catch (IOException e) { } return -1; } static char nc() { try { int b = skip(); if (b == -1) return 0; return (char) b; } catch (IOException e) { } return 0; } static double nd() { try { return Double.parseDouble(ns()); } catch (Exception e) { } return 0; } static String ns() { try { int b = skip(); StringBuilder sb = new StringBuilder(); if (b == -1) return ""; sb.append((char) b); while (true) { b = is.read(); if (b == -1) return sb.toString(); if (b <= ' ') return sb.toString(); sb.append((char) b); } } catch (IOException e) { } return ""; } public static char[] ns(int n) { char[] buf = new char[n]; try { int b = skip(), p = 0; if (b == -1) return null; buf[p++] = (char) b; while (p < n) { b = is.read(); if (b == -1 || b <= ' ') break; buf[p++] = (char) b; } return Arrays.copyOf(buf, p); } catch (IOException e) { } return null; } public static byte[] nse(int n) { byte[] buf = new byte[n]; try { int b = skip(); if (b == -1) return null; is.read(buf); return buf; } catch (IOException e) { } return null; } static int skip() throws IOException { int b; while ((b = is.read()) != -1 && !(b >= 33 && b <= 126)) ; return b; } static boolean eof() { try { is.mark(1000); int b = skip(); is.reset(); return b == -1; } catch (IOException e) { return true; } } static int ni() { try { int num = 0; boolean minus = false; while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-')) ; if (num == '-') { num = 0; minus = true; } else { num -= '0'; } while (true) { int b = is.read(); if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } } } catch (IOException e) { } return -1; } }