結果

問題 No.370 道路の掃除
ユーザー jp_stejp_ste
提出日時 2016-05-14 02:46:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 1,665 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 80,564 KB
実行使用メモリ 41,880 KB
最終ジャッジ日時 2024-04-22 17:41:30
合計ジャッジ時間 7,565 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
41,048 KB
testcase_01 AC 97 ms
39,592 KB
testcase_02 AC 112 ms
40,784 KB
testcase_03 AC 103 ms
40,004 KB
testcase_04 AC 108 ms
40,496 KB
testcase_05 AC 101 ms
39,964 KB
testcase_06 AC 108 ms
41,068 KB
testcase_07 AC 111 ms
40,960 KB
testcase_08 AC 111 ms
41,188 KB
testcase_09 AC 97 ms
40,004 KB
testcase_10 AC 114 ms
40,944 KB
testcase_11 AC 129 ms
41,040 KB
testcase_12 AC 112 ms
40,668 KB
testcase_13 AC 121 ms
41,300 KB
testcase_14 AC 117 ms
41,172 KB
testcase_15 AC 122 ms
41,188 KB
testcase_16 AC 119 ms
41,088 KB
testcase_17 AC 127 ms
41,816 KB
testcase_18 AC 118 ms
40,988 KB
testcase_19 AC 131 ms
41,328 KB
testcase_20 AC 152 ms
41,636 KB
testcase_21 AC 158 ms
41,844 KB
testcase_22 AC 144 ms
41,376 KB
testcase_23 AC 131 ms
41,260 KB
testcase_24 AC 120 ms
41,384 KB
testcase_25 AC 127 ms
41,400 KB
testcase_26 AC 150 ms
41,876 KB
testcase_27 AC 126 ms
41,048 KB
testcase_28 AC 125 ms
41,608 KB
testcase_29 AC 147 ms
41,600 KB
testcase_30 AC 164 ms
41,880 KB
testcase_31 AC 155 ms
41,736 KB
testcase_32 AC 149 ms
41,624 KB
testcase_33 AC 152 ms
41,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            int n = Integer.parseInt(scan.next());
            int m = Integer.parseInt(scan.next());
            ArrayList<Integer> list = new ArrayList<>();
            
            int leftC = 0;
            int rightC = 0;
            for(int i=0; i<m; i++) {
                int v = Integer.parseInt(scan.next());
                if(v > 0) rightC ++;
                if(v < 0) leftC ++;
                list.add(v);
            }
            if( list.indexOf(new Integer(0)) < 0) {
                list.add(new Integer(0));
            } else {
                n--;
            }
            
            Collections.sort(list);
            
            int zeroIndex = list.indexOf(new Integer(0));
            int ans = Integer.MAX_VALUE;
            
            if(zeroIndex == 0) {
                ans = list.get(n);
            } else if(zeroIndex == m) {
                ans = Math.abs(list.get(zeroIndex-n));
            } else {
                for(int i=zeroIndex; i<list.size(); i++) {
                    if(i-n < 0) continue;
                    int right = i;
                    int left = right - n;
                    
                    int v1 = Math.abs(list.get(right)) * 2 + Math.abs(list.get(left));
                    int v2 = Math.abs(list.get(left)) * 2 + Math.abs(list.get(right));
                    ans = Math.min(ans, Math.min(v1, v2));
                }
            }
            System.out.println(ans);
        }
    }
}
0