結果

問題 No.545 ママの大事な二人の子供
ユーザー tentententen
提出日時 2023-01-31 08:43:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 345 ms / 2,000 ms
コード長 2,488 bytes
コンパイル時間 3,740 ms
コンパイル使用メモリ 87,664 KB
実行使用メモリ 72,308 KB
最終ジャッジ日時 2023-09-13 03:38:13
合計ジャッジ時間 9,961 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,576 KB
testcase_01 AC 45 ms
49,652 KB
testcase_02 AC 46 ms
49,624 KB
testcase_03 AC 46 ms
49,632 KB
testcase_04 AC 46 ms
49,668 KB
testcase_05 AC 46 ms
49,532 KB
testcase_06 AC 45 ms
47,656 KB
testcase_07 AC 46 ms
49,796 KB
testcase_08 AC 46 ms
49,680 KB
testcase_09 AC 47 ms
50,076 KB
testcase_10 AC 47 ms
49,524 KB
testcase_11 AC 48 ms
49,832 KB
testcase_12 AC 48 ms
49,696 KB
testcase_13 AC 50 ms
50,092 KB
testcase_14 AC 52 ms
49,868 KB
testcase_15 AC 47 ms
49,736 KB
testcase_16 AC 54 ms
50,348 KB
testcase_17 AC 61 ms
51,296 KB
testcase_18 AC 94 ms
53,220 KB
testcase_19 AC 103 ms
53,468 KB
testcase_20 AC 129 ms
56,028 KB
testcase_21 AC 47 ms
49,996 KB
testcase_22 AC 185 ms
60,836 KB
testcase_23 AC 334 ms
71,812 KB
testcase_24 AC 195 ms
60,548 KB
testcase_25 AC 335 ms
71,720 KB
testcase_26 AC 326 ms
72,024 KB
testcase_27 AC 328 ms
71,784 KB
testcase_28 AC 328 ms
71,688 KB
testcase_29 AC 335 ms
72,308 KB
testcase_30 AC 345 ms
71,908 KB
testcase_31 AC 340 ms
71,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        TreeSet<Long> ev = new TreeSet<>();
        TreeSet<Long> od = new TreeSet<>();
        ev.add(0L);
        od.add(0L);
        for (int i = 0; i < n; i++) {
            TreeSet<Long> tmp;
            if (i % 2 == 0) {
                tmp = ev;
            } else {
                tmp = od;
            }
            TreeSet<Long> next = new TreeSet<>();
            long a = sc.nextLong();
            long b = sc.nextLong();
            for (long x : tmp) {
                next.add(x + a);
                next.add(x - b);
            }
            if (i % 2 == 0) {
                ev  = next;
            } else {
                od = next;
            }
        }
        od.add(Long.MAX_VALUE / 2);
        od.add(Long.MIN_VALUE / 2);
        long ans = Long.MAX_VALUE;
        for (long x : ev) {
            long left = Math.abs(x + od.floor(-x));
            long right = Math.abs(x + od.ceiling(-x));
            ans = Math.min(ans, left);
            ans = Math.min(ans, right);
        }
        System.out.println(ans);
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    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();
    }
    
}
0