結果

問題 No.370 道路の掃除
ユーザー htensaihtensai
提出日時 2019-11-21 10:16:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 1,217 bytes
コンパイル時間 2,050 ms
コンパイル使用メモリ 78,056 KB
実行使用メモリ 42,552 KB
最終ジャッジ日時 2024-04-17 18:33:51
合計ジャッジ時間 8,286 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
41,424 KB
testcase_01 AC 116 ms
41,512 KB
testcase_02 AC 132 ms
41,640 KB
testcase_03 AC 132 ms
41,788 KB
testcase_04 AC 133 ms
41,520 KB
testcase_05 AC 132 ms
41,572 KB
testcase_06 AC 133 ms
41,852 KB
testcase_07 AC 130 ms
41,280 KB
testcase_08 AC 132 ms
41,156 KB
testcase_09 AC 131 ms
41,696 KB
testcase_10 AC 140 ms
41,320 KB
testcase_11 AC 141 ms
41,656 KB
testcase_12 AC 141 ms
41,780 KB
testcase_13 AC 141 ms
41,540 KB
testcase_14 AC 145 ms
41,488 KB
testcase_15 AC 139 ms
41,532 KB
testcase_16 AC 139 ms
41,700 KB
testcase_17 AC 135 ms
41,156 KB
testcase_18 AC 140 ms
41,688 KB
testcase_19 AC 138 ms
41,512 KB
testcase_20 AC 169 ms
42,412 KB
testcase_21 AC 173 ms
42,452 KB
testcase_22 AC 171 ms
42,280 KB
testcase_23 AC 165 ms
42,552 KB
testcase_24 AC 142 ms
41,956 KB
testcase_25 AC 162 ms
42,296 KB
testcase_26 AC 184 ms
42,504 KB
testcase_27 AC 158 ms
41,784 KB
testcase_28 AC 170 ms
42,156 KB
testcase_29 AC 182 ms
42,240 KB
testcase_30 AC 184 ms
42,436 KB
testcase_31 AC 165 ms
41,908 KB
testcase_32 AC 166 ms
42,340 KB
testcase_33 AC 187 ms
42,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[] arr = new int[m];
		for (int i = 0; i < m; i++) {
		    arr[i] = sc.nextInt();
		}
		Arrays.sort(arr);
		int minStart;
		if (arr[0] >= 0) {
		    minStart = 0;
		} else if (arr[m - 1] < 0) {
		    minStart = m;
		} else {
    		int left = 0; 
    		int right = arr.length - 1;
    		while (right - left > 1) {
    		    int mm = (left + right) / 2;
    		    if (arr[mm] >= 0) {
    		        right = mm;
    		    } else {
    		        left = mm;
    		    }
    		}
    		minStart = right;
		}
		int min = Integer.MAX_VALUE;
		for (int i = 0; i <= n; i++) {
		    if (minStart - i < 0 || minStart + n - i - 1 > m - 1) {
		        continue;
		    }
		    if (i == 0) {
		        min = Math.min(min, arr[minStart + n - i - 1]);
		    } else if (i == n) {
		        min = Math.min(min, - arr[minStart - i]);
		    } else {
    		    min = Math.min(min, - arr[minStart - i] * 2 + arr[minStart + n - i - 1]);
    		    min = Math.min(min, - arr[minStart - i] + arr[minStart + n - i - 1] * 2);
		    }
		}
	    System.out.println(min);
	}
}
0