結果

問題 No.2542 Yokan for Two
ユーザー tenten
提出日時 2024-04-05 17:54:06
言語 Java
(openjdk 23)
結果
AC  
実行時間 462 ms / 2,000 ms
コード長 1,882 bytes
コンパイル時間 2,977 ms
コンパイル使用メモリ 79,636 KB
実行使用メモリ 57,376 KB
最終ジャッジ日時 2024-10-01 01:23:43
合計ジャッジ時間 12,511 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int length = sc.nextInt();
        int n = sc.nextInt();
        int[] positions = new int[n];
        for (int i = 0; i < n; i++) {
            positions[i] = sc.nextInt();
        }
        int base = Math.abs(length - positions[n - 1] - positions[0]);
        Set<Integer> current = new HashSet<>();
        current.add(0);
        for (int i = 0; i < n - 1; i++) {
            int x = positions[i + 1] - positions[i];
            Set<Integer> next = new HashSet<>();
            for (int y : current) {
                next.add(x + y);
                next.add(Math.abs(y - x));
            }
            current = next;
        }
        int ans = Integer.MAX_VALUE;
        for (int x : current) {
            ans = Math.min(ans, Math.abs(base - x));
        }
        System.out.println(ans);
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0