시배's Android

Algorithm | 백준 Kotlin 3273 두 수의 합 본문

Algorithm

Algorithm | 백준 Kotlin 3273 두 수의 합

si8ae 2023. 10. 7. 17:04
 

3273번: 두 수의 합

n개의 서로 다른 양의 정수 a1, a2, ..., an으로 이루어진 수열이 있다. ai의 값은 1보다 크거나 같고, 1000000보다 작거나 같은 자연수이다. 자연수 x가 주어졌을 때, ai + aj = x (1 ≤ i < j ≤ n)을 만족하는

www.acmicpc.net

 

private fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map{ it.toInt() }
    val x = readLine()!!.toInt()
    val f = a.groupBy{ it }.map{ (a, b) -> a to b.size }.toMap()
    println(f.map{ (y, c) ->  c.toLong() * f.getOrElse(x - y) { 0 } }.sum() / 2)
}