結果
問題 |
No.723 2つの数の和
|
ユーザー |
![]() |
提出日時 | 2020-09-02 10:09:21 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 784 ms / 2,000 ms |
コード長 | 1,391 bytes |
コンパイル時間 | 2,213 ms |
コンパイル使用メモリ | 78,848 KB |
実行使用メモリ | 56,168 KB |
最終ジャッジ日時 | 2024-11-21 14:04:15 |
合計ジャッジ時間 | 15,036 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 22 |
ソースコード
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); } }