結果

問題 No.957 植林
ユーザー htensai
提出日時 2020-02-12 11:33:54
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,538 bytes
コンパイル時間 2,717 ms
コンパイル使用メモリ 80,552 KB
実行使用メモリ 61,300 KB
最終ジャッジ日時 2024-10-03 05:31:26
合計ジャッジ時間 32,668 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22 WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static final int MOD = 1000000007;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int w = sc.nextInt();
        int[][] field = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                field[i][j] = sc.nextInt();
            }
        }
        int[] rows = new int[h];
        PriorityQueue<Type> queue = new PriorityQueue<>();
        PriorityQueue<Type> next = new PriorityQueue<>();
        for (int i = 0; i < h; i++) {
            rows[i] = sc.nextInt();
            long value = rows[i];
            for (int j = 0; j < w; j++) {
                value -= field[i][j];
            }
            queue.add(new Type(true, i, value));
        }
        int[] cals = new int[w];
        for (int i = 0; i < w; i++) {
            cals[i] = sc.nextInt();
            long value = cals[i];
            for (int j = 0; j < h; j++) {
                value -= field[j][i];
            }
            queue.add(new Type(false, i, value));
        }
        long total = 0;
        long max = 0;
        while (queue.size() > 0) {
            Type target = queue.poll();
            total += target.value;
            max = Math.max(max, total);
            while (queue.size() > 0) {
                Type tmp = queue.poll();
                if (target.isRow != tmp.isRow) {
                    if (target.isRow) {
                        tmp.value += field[target.idx][tmp.idx];
                    } else {
                        tmp.value += field[tmp.idx][target.idx];
                    }
                }
                next.add(tmp);
            }
            PriorityQueue<Type> t = next;
            next = queue;
            queue = t;
        }
        System.out.println(max);
    }
    
    static class Type implements Comparable<Type> {
        boolean isRow;
        int idx;
        long value;
        
        public Type(boolean isRow, int idx, long value) {
            this.isRow = isRow;
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Type another) {
            if (value < another.value) {
                return 1;
            } else if (value == another.value) {
                return 0;
            } else {
                return -1;
            }
        }
        
        public String toString() {
            return isRow + ":" + idx + ":" + value;
        }
   }
}
0