結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー yukirinyukirin
提出日時 2016-03-04 02:11:04
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 1,164 bytes
コンパイル時間 13,063 ms
コンパイル使用メモリ 241,440 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-04-19 06:55:45
合計ジャッジ時間 11,443 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
権限があれば一括ダウンロードができます

ソースコード

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 := make(map[int]int)
		for i := 0; i < n; i++ {
			m[nextInt()]++
		}

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

		for q.Len() > 0 {
			kd := make([]int, 0, 3)
			for i := 0; i < 3; i++ {
				if q.Len() == 0 {
					break
				}
				v := heap.Pop(&q).([2]int)
				v[1]--
				if v[1] > 0 {
					heap.Push(&q, v)
				}
				kd = append(kd, v[0])
			}
			if len(kd) < 3 {
				break
			}
			c++
		}
		fmt.Println(c)
	}
}

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

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

type pQ [][2]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.([2]int))
}

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