結果

問題 No.135 とりあえず1次元の問題
ユーザー tsunabittsunabit
提出日時 2018-05-06 13:07:33
言語 Java19
(openjdk 21)
結果
AC  
実行時間 491 ms / 5,000 ms
コード長 2,110 bytes
コンパイル時間 3,868 ms
コンパイル使用メモリ 77,804 KB
実行使用メモリ 67,872 KB
最終ジャッジ日時 2023-09-10 10:44:29
合計ジャッジ時間 10,413 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 491 ms
67,040 KB
testcase_01 AC 129 ms
55,952 KB
testcase_02 AC 128 ms
55,868 KB
testcase_03 AC 127 ms
56,152 KB
testcase_04 AC 129 ms
56,084 KB
testcase_05 AC 131 ms
55,956 KB
testcase_06 AC 130 ms
55,636 KB
testcase_07 AC 130 ms
55,972 KB
testcase_08 AC 131 ms
55,524 KB
testcase_09 AC 131 ms
56,292 KB
testcase_10 AC 132 ms
55,908 KB
testcase_11 AC 131 ms
56,048 KB
testcase_12 AC 142 ms
55,840 KB
testcase_13 AC 130 ms
55,852 KB
testcase_14 AC 161 ms
55,728 KB
testcase_15 AC 142 ms
56,168 KB
testcase_16 AC 165 ms
55,996 KB
testcase_17 AC 160 ms
55,928 KB
testcase_18 AC 131 ms
58,036 KB
testcase_19 AC 163 ms
56,236 KB
testcase_20 AC 148 ms
56,048 KB
testcase_21 AC 436 ms
67,872 KB
testcase_22 AC 451 ms
66,868 KB
evil01.txt AC 500 ms
66,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.stream.Stream;
import java.util.Arrays;

// ***問題文***
// 数直線上の整数座標上にN個の点がある。
// その中から同じ座標ではない2点を選んで、その2点の距離を求める。
// 距離は、i番目の点の座標をXi、j番目の点の座標をXjとすると 、
// 絶対値|Xi−Xj|とする。
// この時、最小の距離となる2点を選ぶとして、選んだ2点間の最小距離を求めてください。
// 条件にあう2点を選べなかったら0を出力してください。
// ***入力***
// N
// X1 X2 … XN
// 入力は全て整数で与えられる。
// ・1≤N≤100000=105
// ・0≤Xi≤1000000=106,1≤i≤N
// ***出力***
// 条件にあう2点間の最小距離を求めてください。
// 2点を選べなかったら0を出力してください。
// 最後に改行してください。

public class No135 {
    public static void main(String[] args) {
        // 標準入力から読み込む際に、Scannerオブジェクトを使う。
        Scanner sc = new Scanner(System.in);
        // 2行目をnextLineで読み込むため、数値もnextLineで読み込む
        int n = Integer.parseInt(sc.nextLine());
        // 新しいstreamを作成し、各要素をintに変換
        int[] x = Stream.of(sc.nextLine().split(" " , 0)).mapToInt(Integer::parseInt).toArray();
        Arrays.sort(x);
        // intの最大値をラッパークラスで取得
        int min = Integer.MAX_VALUE;

        for(int i = 0; i < x.length - 1; i++) {
            if((x[i] != x[i + 1]) && Math.abs(x[i] - x[i + 1]) < min) {
                min = Math.abs(x[i] - x[i + 1]);
            }
        }
        if(min == Integer.MAX_VALUE) {
            min = 0;
        }
        System.out.println(min);
        // ----------
        // System.out.println("n = " + n);
        // for(int i = 0; i < x.length; i++) {
        //     System.out.println("[" + i + "] = " + x[i]);
        // }
        // for(int a: x) {
        //     System.out.println("x = " + x);
        // }
    }
}
0