結果

問題 No.135 とりあえず1次元の問題
ユーザー YamaKasaYamaKasa
提出日時 2018-04-25 01:32:51
言語 Java
(openjdk 23)
結果
AC  
実行時間 838 ms / 5,000 ms
コード長 1,582 bytes
コンパイル時間 2,721 ms
コンパイル使用メモリ 79,516 KB
実行使用メモリ 51,312 KB
最終ジャッジ日時 2024-06-27 20:51:40
合計ジャッジ時間 9,853 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		//int []X = new int[N];
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			list.add(scan.nextInt());
		}
		scan.close();
		if(N == 1) {
			System.out.println("0");
			System.exit(0);
		}
		Collections.sort(list);

		int k = 0;
		for(int i = 0; i < N - 1; i++) {
			if(list.get(i) == list.get(i + 1)) {
				k ++;
			}else {
				break;
			}
		}
		if(k == N - 1) {
			System.out.println("0");
			System.exit(0);
		}
		int ans = list.get(k) - list.get(k + 1);
		if(ans < 0) {
			ans = - ans;
		}
		int temp = 0;
		for(int i = k + 1; i < N - 1; i++) {
			int x1 = list.get(i);
			int x2 = list.get(i + 1);
			temp = x1 - x2;
			if(temp != 0) {
				if(temp < 0) {
					temp = - temp;
				}
				if(ans > temp) {
					ans = temp;
				}
			}
		}
//		Arrays.sort(X);
//		int k = 0;
//		for(int i = 0; i < N - 1; i++) {
//			if(X[i] == X[i + 1]) {
//				k ++;
//			}else {
//				break;
//			}
//		}
//		if(k == N - 1) {
//			System.out.println("0");
//			System.exit(0);
//		}
//		int ans = X[k] - X[k + 1];
//		if(ans < 0) {
//			ans = - ans;
//		}
//		int temp = 0;
//		for(int i = k + 1; i < N - 1; i++) {
//			int x1 = X[i];
//			int x2 = X[i + 1];
//			temp = x1 - x2;
//			if(temp != 0) {
//				if(temp < 0) {
//					temp = - temp;
//				}
//				if(ans > temp) {
//					ans = temp;
//				}
//			}
//		}
		System.out.println(ans);
	}
}
0