結果

問題 No.1823 Tricolor Dango
ユーザー HimaHima
提出日時 2022-05-15 22:15:43
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 1,873 bytes
コンパイル時間 12,404 ms
コンパイル使用メモリ 208,260 KB
実行使用メモリ 14,408 KB
最終ジャッジ日時 2023-10-11 11:20:43
合計ジャッジ時間 17,348 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 68 ms
8,684 KB
testcase_12 WA -
testcase_13 AC 63 ms
8,192 KB
testcase_14 AC 70 ms
8,224 KB
testcase_15 AC 58 ms
8,192 KB
testcase_16 AC 24 ms
7,852 KB
testcase_17 AC 36 ms
7,884 KB
testcase_18 AC 71 ms
10,144 KB
testcase_19 AC 187 ms
14,408 KB
testcase_20 AC 70 ms
10,148 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)

type PriorityQueue []int

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

func (pq *PriorityQueue) Push(item interface{}) {
	*pq = append(*pq, item.(int))
}

func (pq *PriorityQueue) Pop() interface{} {
	es := *pq // EdgeのSlice
	n := len(es)
	item := es[n-1]
	*pq = es[0 : n-1]
	return item
}

func solve(t int, n []int, a [][]int) []string {
	var ans []string
	for k := 0; k < t; k++ {
		q := &PriorityQueue{}
		heap.Init(q)
		for _, v := range a[k] {
			heap.Push(q, v)
		}
		for q.Len() >= 3 {
			var b []int
			for j := 0; j < 3; j++ {
				b = append(b, heap.Pop(q).(int))
			}
			sort.Ints(b)
			b[1] -= b[0]
			b[2] -= b[0]
			if b[1] > 0 {
				heap.Push(q, b[1])
			}
			if b[2] > 0 {
				heap.Push(q, b[2])
			}
		}
		if q.Len() == 0 {
			ans = append(ans, "Yes")
		} else {
			ans = append(ans, "No")
		}
	}
	return ans
}

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	t := nextInt()

	var n []int
	var a [][]int
	for i := 0; i < t; i++ {
		ni := nextInt()
		n = append(n, ni)
		a = append(a, nextIntSlice(ni))
	}
	ans := solve(t, n, a)

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	for _, v := range ans {
		fmt.Fprintln(out, v)
	}
}

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func nextIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = nextInt()
	}
	return s
}

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func Min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func Max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
0