結果
| 問題 | 
                            No.2157 崖
                             | 
                    
| コンテスト | |
| ユーザー | 
                             tenten
                         | 
                    
| 提出日時 | 2022-12-26 10:21:44 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,378 bytes | 
| コンパイル時間 | 2,660 ms | 
| コンパイル使用メモリ | 84,176 KB | 
| 実行使用メモリ | 129,284 KB | 
| 最終ジャッジ日時 | 2024-11-20 18:43:27 | 
| 合計ジャッジ時間 | 78,651 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 TLE * 10 | 
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
    static int[][] matrix;
    static ArrayList<HashMap<Integer, Integer>> dp = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        matrix = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = sc.nextInt();
            }
            dp.add(new HashMap<>());
        }
        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < m; i++) {
            ans = Math.min(ans, dfw(n - 2, matrix[n - 1][i]));
        }
        if (ans >= Integer.MAX_VALUE / 2) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    static int dfw(int idx, int v) {
        if (idx < 0) {
            return 0;
        }
        if (!dp.get(idx).containsKey(v)) {
            int ans = Integer.MAX_VALUE / 2;
            for (int x : matrix[idx]) {
                if (x <= v) {
                    ans = Math.min(ans, Math.max(dfw(idx - 1, x), v - x));
                }
            }
            dp.get(idx).put(v, ans);
        }
        return dp.get(idx).get(v);
    }
}
class Utilities {
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
            
            
            
        
            
tenten