結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー yukirinyukirin
提出日時 2016-03-04 02:29:45
言語 Go
(1.22.1)
結果
AC  
実行時間 48 ms / 5,000 ms
コード長 1,165 bytes
コンパイル時間 11,261 ms
コンパイル使用メモリ 223,420 KB
実行使用メモリ 8,128 KB
最終ジャッジ日時 2024-04-19 06:56:35
合計ジャッジ時間 11,960 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
8,088 KB
testcase_01 AC 47 ms
8,080 KB
testcase_02 AC 33 ms
8,128 KB
testcase_03 AC 48 ms
8,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Split(bufio.ScanWords)
	t := nextInt()

	for p := 0; p < t; p++ {
		n, c := nextInt(), 0
		m, q := make(map[int]int), make(pQ, 0, 1000)
		for i := 0; i < n; i++ {
			m[nextInt()]++
		}

		heap.Init(&q)
		for k, v := range m {
			heap.Push(&q, []int{k, v})
		}

		for {
			kd := make([][]int, 0, 3)
			for i := 0; i < 3; i++ {
				if q.Len() == 0 {
					break
				}
				kd = append(kd, heap.Pop(&q).([]int))
			}
			if len(kd) < 3 {
				break
			}

			for _, v := range kd {
				v[1]--
				if v[1] > 0 {
					heap.Push(&q, v)
				}
			}
			c++
		}
		fmt.Println(c)
	}
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, _ := strconv.Atoi(nextLine())
	return i
}

type pQ [][]int

func (h pQ) Len() int {
	return len(h)
}

func (h pQ) Less(i, j int) bool {
	return h[i][1] > h[j][1]
}

func (h pQ) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *pQ) Push(x interface{}) {
	*h = append(*h, x.([]int))
}

func (h *pQ) Pop() interface{} {
	x := (*h)[len(*h)-1]
	*h = (*h)[0 : len(*h)-1]
	return x
}
0