結果

問題 No.1479 Matrix Eraser
ユーザー tentententen
提出日時 2021-04-28 09:49:55
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,380 bytes
コンパイル時間 3,476 ms
コンパイル使用メモリ 77,744 KB
実行使用メモリ 147,028 KB
最終ジャッジ日時 2023-09-21 10:35:12
合計ジャッジ時間 37,091 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
49,632 KB
testcase_01 AC 41 ms
49,664 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 341 ms
66,100 KB
testcase_08 AC 496 ms
76,156 KB
testcase_09 AC 869 ms
101,636 KB
testcase_10 AC 1,505 ms
124,712 KB
testcase_11 AC 1,088 ms
107,380 KB
testcase_12 AC 419 ms
68,280 KB
testcase_13 AC 496 ms
70,832 KB
testcase_14 AC 429 ms
67,828 KB
testcase_15 AC 183 ms
58,096 KB
testcase_16 AC 443 ms
68,100 KB
testcase_17 AC 1,652 ms
143,868 KB
testcase_18 AC 1,674 ms
141,368 KB
testcase_19 AC 1,869 ms
146,864 KB
testcase_20 AC 1,725 ms
141,608 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 1,690 ms
141,240 KB
testcase_24 AC 1,841 ms
146,700 KB
testcase_25 WA -
testcase_26 AC 1,876 ms
147,028 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 182 ms
58,204 KB
testcase_38 AC 787 ms
110,376 KB
testcase_39 AC 1,318 ms
146,172 KB
testcase_40 AC 41 ms
49,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        ArrayList<HashMap<Integer, Integer>> rows = new ArrayList<>();
        ArrayList<HashMap<Integer, Integer>> cols = new ArrayList<>();
        for (int i = 0; i < w; i++) {
            cols.add(new HashMap<>());
        }
        for (int i = 0; i < h; i++) {
            rows.add(new HashMap<>());
            for (int j = 0; j < w; j++) {
                int x = sc.nextInt();
                if (x == 0) {
                    continue;
                }
                rows.get(i).put(x, rows.get(i).getOrDefault(x, 0) + 1);
                cols.get(j).put(x, cols.get(j).getOrDefault(x, 0) + 1);
            }
        }
        TreeMap<Integer, PriorityQueue<Unit>> all = new TreeMap<>();
        for (int i = 0; i < h; i++) {
            for (Map.Entry<Integer, Integer> entry : rows.get(i).entrySet()) {
                int key = entry.getKey();
                int value = entry.getValue();
                if (!all.containsKey(-key)) {
                    all.put(-key, new PriorityQueue<>());
                }
                all.get(-key).add(new Unit(value, true));
            }
        }
        for (int i = 0; i < w; i++) {
            for (Map.Entry<Integer, Integer> entry : cols.get(i).entrySet()) {
                int key = entry.getKey();
                int value = entry.getValue();
                if (!all.containsKey(-key)) {
                    all.put(-key, new PriorityQueue<>());
                }
                all.get(-key).add(new Unit(value, false));
            }
        }
        int ans = 0;
        for (PriorityQueue<Unit> queue : all.values()) {
            int rowCount = 0;
            int colCount = 0;
            while (queue.size() > 0) {
                Unit u = queue.poll();
                if (u.isRow) {
                    if (u.value > colCount) {
                        ans++;
                        rowCount++;
                    }
                } else {
                    if (u.value > rowCount) {
                        ans++;
                        colCount++;
                    }
                }
            }
        }
        System.out.println(ans);
    }
    
    static class Unit implements Comparable<Unit> {
        int value;
        boolean isRow;
        
        public Unit(int value, boolean isRow) {
            this.isRow = isRow;
            this.value = value;
        }
        
        public int compareTo(Unit another) {
            return another.value - value;
        }
        
        public String toString() {
            return value + ":" + isRow;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0