結果

問題 No.1238 選抜クラス
ユーザー tenten
提出日時 2021-12-01 08:52:09
言語 Java
(openjdk 23)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 2,069 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 79,264 KB
実行使用メモリ 60,264 KB
最終ジャッジ日時 2024-07-04 07:33:08
合計ジャッジ時間 8,139 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt() - k;
        }
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            if (values[i] < 0) {
                Integer key = Integer.MIN_VALUE;
                while ((key = map.higherKey(key)) != null) {
                    int next = key + values[i];
                    map.put(next, (map.getOrDefault(next, 0) + map.get(key)) % MOD);
                }
            } else {
                Integer key = Integer.MAX_VALUE;
                while ((key = map.lowerKey(key)) != null) {
                    int next = key + values[i];
                    map.put(next, (map.getOrDefault(next, 0) + map.get(key)) % MOD);
                }
           }
           map.put(values[i], (map.getOrDefault(values[i], 0) + 1) % MOD);
        }
        int ans = 0;
        for (int x : map.keySet()) {
            if (x < 0) {
                continue;
            }
            ans += map.get(x);
            ans %= MOD;
        }
        System.out.println(ans);
    }
    
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0