結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー fmhrfmhr
提出日時 2015-05-29 13:21:33
言語 Go1.4
(1.4.2)
結果
AC  
実行時間 89 ms / 5,000 ms
コード長 1,433 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 34,700 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-03 20:22:08
合計ジャッジ時間 995 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
6,816 KB
testcase_01 AC 89 ms
6,940 KB
testcase_02 AC 44 ms
6,940 KB
testcase_03 AC 89 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

///heap/////////////////////////////////

// An IntHeap is a min-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

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

////nextInt////////////////////////////
var s = bufio.NewScanner(os.Stdin)

func next() string {
	s.Split(bufio.ScanWords)
	s.Scan()
	return s.Text()
}
func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return int(i)
}

/////solve///////////////////////////////////////
func solve() {
	N := nextInt()
	m := make(map[int]int)
	for i := 0; i < N; i++ {
		z := nextInt()
		m[z]++
	}
	h := &IntHeap{}
	for _, v := range m {
		heap.Push(h, v)
	}
	var ans int
	for h.Len() >= 3 {
		a := make([]int, 3)
		ans += 1
		for i := 0; i < 3; i++ {
			a[i] = heap.Pop(h).(int) - 1
		}
		//fmt.Println(a)
		for i := 0; i < 3; i++ {
			if a[i] > 0 {
				heap.Push(h, a[i])
			}
		}
	}
	fmt.Println(ans)
}

func main() {
	T := nextInt()
	for i := 0; i < T; i++ {
		solve()
	}
}
0