結果

問題 No.723 2つの数の和
ユーザー tentententen
提出日時 2023-02-15 19:25:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 420 ms / 2,000 ms
コード長 2,567 bytes
コンパイル時間 2,476 ms
コンパイル使用メモリ 91,156 KB
実行使用メモリ 61,276 KB
最終ジャッジ日時 2023-09-25 03:40:52
合計ジャッジ時間 10,314 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,560 KB
testcase_01 AC 44 ms
49,500 KB
testcase_02 AC 44 ms
49,368 KB
testcase_03 AC 367 ms
58,584 KB
testcase_04 AC 416 ms
58,924 KB
testcase_05 AC 383 ms
58,280 KB
testcase_06 AC 420 ms
59,080 KB
testcase_07 AC 282 ms
56,712 KB
testcase_08 AC 294 ms
56,844 KB
testcase_09 AC 411 ms
59,024 KB
testcase_10 AC 282 ms
56,656 KB
testcase_11 AC 134 ms
53,340 KB
testcase_12 AC 323 ms
56,812 KB
testcase_13 AC 391 ms
58,340 KB
testcase_14 AC 185 ms
55,448 KB
testcase_15 AC 272 ms
56,508 KB
testcase_16 AC 328 ms
58,684 KB
testcase_17 AC 346 ms
58,604 KB
testcase_18 AC 199 ms
56,264 KB
testcase_19 AC 231 ms
56,272 KB
testcase_20 AC 44 ms
49,472 KB
testcase_21 AC 43 ms
49,496 KB
testcase_22 AC 44 ms
49,616 KB
testcase_23 AC 396 ms
61,276 KB
testcase_24 AC 238 ms
56,104 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();
        int target = sc.nextInt();
        TreeMap<Integer, Long> counts = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            counts.put(x, counts.getOrDefault(x, 0L) + 1);
        }
        Integer left = Integer.MIN_VALUE;
        Integer right = Integer.MAX_VALUE;
        long ans = 0;
        while ((left = counts.higherKey(left)) != null) {
            if (counts.containsKey(right)) {
                if (left + right == target) {
                    ans += counts.get(left) * counts.get(right);
                    continue;
                } else if (left + right < target) {
                    continue;
                }
            }
            while ((right = counts.lowerKey(right)) != null) {
                if (left + right == target) {
                    ans += counts.get(left) * counts.get(right);
                    break;
                } else if (left + right < target) {
                    break;
                }
            }
            if (right == null) {
                break;
            }
        }
        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