提出 #68284927


ソースコード 拡げる

// Go 1.20.6
// 処理時間: O(N)
// メモリ使用量: O(U) (U は配列内の異なる値の数、最大で N)
package main

import (
	"bufio"
	"fmt"
	"os"
)

// countPairs は、1 ≤ j < i ≤ N かつ A_j = A_i を満たす (i, j) の組数を返します。
// N: 整数の個数
// arr: 整数スライス A1...AN
// 戻り値: 条件を満たす (i, j) の組数
func countPairs(N int, arr []int) int {
	freq := make(map[int]int) // 値 -> 出現回数
	count := 0

	for _, val := range arr {
		if c, exists := freq[val]; exists {
			// 過去の出現回数分だけ組が作れる
			count += c
			freq[val] = c + 1
		} else {
			freq[val] = 1
		}
	}
	return count
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var N int
	fmt.Fscan(in, &N)

	arr := make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Fscan(in, &arr[i])
	}

	result := countPairs(N, arr)
	fmt.Println(result)
}

提出情報

提出日時
問題 B54 - Counting Same Values
ユーザ myoshizumi
言語 Go (go 1.20.6)
得点 1000
コード長 935 Byte
結果 AC
実行時間 52 ms
メモリ 8584 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 1000 / 1000
結果
AC × 1
AC × 8
セット名 テストケース
Sample sample_01
All max_01, max_02, random_01, random_02, random_03, random_04, random_05, sample_01
ケース名 結果 実行時間 メモリ
max_01 AC 26 ms 2924 KiB
max_02 AC 30 ms 3320 KiB
random_01 AC 0 ms 1716 KiB
random_02 AC 0 ms 1740 KiB
random_03 AC 1 ms 1852 KiB
random_04 AC 6 ms 2668 KiB
random_05 AC 52 ms 8584 KiB
sample_01 AC 1 ms 1712 KiB