結果
問題 | No.120 傾向と対策:門松列(その1) |
ユーザー | neko_the_shadow |
提出日時 | 2020-03-06 16:01:14 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,628 bytes |
コンパイル時間 | 10,948 ms |
コンパイル使用メモリ | 220,708 KB |
実行使用メモリ | 8,156 KB |
最終ジャッジ日時 | 2024-10-14 02:52:58 |
合計ジャッジ時間 | 11,790 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
ソースコード
package main import ( "bufio" "fmt" "math" "os" "strconv" ) func exec(stdin *Stdin, stdout *Stdout) { t := stdin.ReadInt() for i := 0; i < t; i++ { n := stdin.ReadInt() q := NewHeapq() for j := 0; j < n; j++ { l := stdin.ReadInt() q.Push(l, l) } ans := 0 for { a := []int{-1} b := []int{} for len(a) < 4 { if q.isEmpty() { break } c := q.Pop().(int) if a[len(a)-1] == c { b = append(b, c) } else { a = append(a, c) } } if len(a) < 4 { break } ans++ for _, v := range b { q.Push(v, v) } } stdout.Println(ans) } } func main() { stdout := NewStdout() defer stdout.Flush() exec(NewStdin(bufio.ScanWords), stdout) } type Stdin struct { stdin *bufio.Scanner } func NewStdin(split bufio.SplitFunc) *Stdin { s := Stdin{bufio.NewScanner(os.Stdin)} s.stdin.Split(split) s.stdin.Buffer(make([]byte, bufio.MaxScanTokenSize), int(math.MaxInt32)) return &s } func (s *Stdin) Read() string { s.stdin.Scan() return s.stdin.Text() } func (s *Stdin) ReadInt() int { n, _ := strconv.Atoi(s.Read()) return n } func (s *Stdin) ReadFloat64() float64 { n, _ := strconv.ParseFloat(s.Read(), 64) return n } type Stdout struct { stdout *bufio.Writer } func NewStdout() *Stdout { return &Stdout{bufio.NewWriter(os.Stdout)} } func (s *Stdout) Flush() { s.stdout.Flush() } func (s *Stdout) Println(a ...interface{}) { fmt.Fprintln(s.stdout, a...) } type Heapq struct { elements []*element } type element struct { x int v interface{} } func NewHeapq() *Heapq { return &Heapq{elements: []*element{nil}} } func (heapq *Heapq) Push(x int, v interface{}) { heapq.elements = append(heapq.elements, &element{x: x, v: v}) n := len(heapq.elements) - 1 for n > 1 { if heapq.elements[n/2].x < heapq.elements[n].x { break } heapq.elements[n/2], heapq.elements[n] = heapq.elements[n], heapq.elements[n/2] n /= 2 } } func (heapq *Heapq) Pop() interface{} { head := heapq.elements[1].v heapq.elements[1] = heapq.elements[len(heapq.elements)-1] heapq.elements = heapq.elements[:len(heapq.elements)-1] n := 1 for n < len(heapq.elements) { if n*2 >= len(heapq.elements) { break } var m int if n*2+1 >= len(heapq.elements) { m = n * 2 } else { if heapq.elements[n*2].x < heapq.elements[n*2+1].x { m = n * 2 } else { m = n*2 + 1 } } if heapq.elements[n].x < heapq.elements[m].x { break } heapq.elements[n], heapq.elements[m] = heapq.elements[m], heapq.elements[n] n = m } return head } func (heapq *Heapq) isEmpty() bool { return len(heapq.elements) == 1 }