結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
6,812 KB
testcase_01 AC 71 ms
6,944 KB
testcase_02 AC 38 ms
6,940 KB
testcase_03 AC 71 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(l []int) {
	m := make(map[int]int)
	//fmt.Println(l)
	for _, v := range l {
		m[v] += 1
	}
	//fmt.Println(m)
	h := &IntHeap{}
	for _, p := range m {
		heap.Push(h, p)
	}
	heap.Init(h)
	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()
	L := make([][]int, T)
	for i := 0; i < T; i++ {
		N := nextInt()
		L[i] = make([]int, N)
		for j := 0; j < N; j++ {
			L[i][j] = nextInt()
		}
	}

	for i := 0; i < T; i++ {
		solve(L[i])
	}
}
0