結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー fmhrfmhr
提出日時 2015-05-28 00:20:15
言語 Go
(1.22.1)
結果
RE  
実行時間 -
コード長 1,923 bytes
コンパイル時間 12,496 ms
コンパイル使用メモリ 224,060 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 02:08:16
合計ジャッジ時間 12,425 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

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

////pq///////////////////////////////

type Item struct {
	value    int
	priority int
	index    int
}

type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

func (pq *PriorityQueue) update(item *Item, value int, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

////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() {
	var N int
	fmt.Scan(&N)
	var L int
	m := make(map[int]int)
	for i := 0; i < N; i++ {
		L = nextInt()
		m[L] += 1
	}
	//fmt.Println(m)
	pq := make(PriorityQueue, len(m))
	i := 0
	for v, p := range m {
		pq[i] = &Item{
			value:    v,
			priority: p,
			index:    i,
		}
		i++
	}
	heap.Init(&pq)
	var ans int
	for pq.Len() >= 3 {
		a := make(map[int]int)
		ans += 1
		for i := 0; i < 3; i++ {
			item := heap.Pop(&pq).(*Item)
			a[item.value] = item.priority - 1
		}
		//fmt.Println(a)
		for v, p := range a {
			if p > 0 {
				item := &Item{
					value:    v,
					priority: p,
				}
				heap.Push(&pq, item)
			}
		}
	}
	fmt.Println(ans)
}

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}
0