結果

問題 No.723 2つの数の和
ユーザー tentententen
提出日時 2020-09-02 10:09:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 769 ms / 2,000 ms
コード長 1,391 bytes
コンパイル時間 2,991 ms
コンパイル使用メモリ 76,096 KB
実行使用メモリ 67,708 KB
最終ジャッジ日時 2023-08-13 17:46:30
合計ジャッジ時間 16,518 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,780 KB
testcase_01 AC 124 ms
55,868 KB
testcase_02 AC 127 ms
56,192 KB
testcase_03 AC 637 ms
64,492 KB
testcase_04 AC 769 ms
67,260 KB
testcase_05 AC 702 ms
67,124 KB
testcase_06 AC 753 ms
67,708 KB
testcase_07 AC 503 ms
62,924 KB
testcase_08 AC 554 ms
62,704 KB
testcase_09 AC 764 ms
67,108 KB
testcase_10 AC 503 ms
60,876 KB
testcase_11 AC 279 ms
60,128 KB
testcase_12 AC 559 ms
63,152 KB
testcase_13 AC 766 ms
67,216 KB
testcase_14 AC 395 ms
60,472 KB
testcase_15 AC 528 ms
63,468 KB
testcase_16 AC 609 ms
63,172 KB
testcase_17 AC 715 ms
65,480 KB
testcase_18 AC 495 ms
60,988 KB
testcase_19 AC 578 ms
61,844 KB
testcase_20 AC 127 ms
55,792 KB
testcase_21 AC 127 ms
55,928 KB
testcase_22 AC 125 ms
56,008 KB
testcase_23 AC 656 ms
66,152 KB
testcase_24 AC 424 ms
60,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            int a = sc.nextInt();
            if (map.containsKey(a)) {
                map.put(a, map.get(a) + 1);
            } else {
                map.put(a, 1);
            }
        }
        long total = 0;
        if (x % 2 == 0 && map.containsKey(x / 2)) {
            int sum = map.get(x / 2);
            total += (long)sum * sum;
            map.remove(x / 2);
        }
        int length = map.size();
        int[] values = new int[length];
        int[] counts = new int[length];
        int idx = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            values[idx] = entry.getKey();
            counts[idx] = entry.getValue();
            idx++;
        }
        int left = 0;
        int right = length - 1;
        while (left < right) {
            while (left < right && values[right] + values[left] > x) {
                right--;
            }
            if (values[right] + values[left] == x) {
                total += (long)counts[left] * counts[right] * 2;
            }
            left++;
        }
        System.out.println(total);
    }
    
}
0