結果
問題 |
No.545 ママの大事な二人の子供
|
ユーザー |
![]() |
提出日時 | 2023-01-31 08:43:25 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 383 ms / 2,000 ms |
コード長 | 2,488 bytes |
コンパイル時間 | 3,248 ms |
コンパイル使用メモリ | 96,292 KB |
実行使用メモリ | 71,592 KB |
最終ジャッジ日時 | 2024-06-30 13:53:44 |
合計ジャッジ時間 | 9,384 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
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(); } }