結果
| 問題 |
No.120 傾向と対策:門松列(その1)
|
| コンテスト | |
| ユーザー |
fmhr
|
| 提出日時 | 2015-05-29 13:01:15 |
| 言語 | Go1.4 (1.4.2) |
| 結果 |
AC
|
| 実行時間 | 71 ms / 5,000 ms |
| コード長 | 1,609 bytes |
| コンパイル時間 | 292 ms |
| コンパイル使用メモリ | 35,196 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-25 00:21:28 |
| 合計ジャッジ時間 | 1,043 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 |
ソースコード
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])
}
}
fmhr