結果

問題 No.2596 Christmas Eve (Heuristic ver.)
ユーザー ID 21712
提出日時 2025-03-06 23:24:04
言語 Go
(1.23.4)
結果
WA  
実行時間 -
コード長 2,486 bytes
コンパイル時間 12,655 ms
コンパイル使用メモリ 246,324 KB
実行使用メモリ 8,612 KB
スコア 0
最終ジャッジ日時 2025-03-06 23:24:32
合計ジャッジ時間 28,264 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 125
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import . "fmt"
import "sort"

type Unit struct {
	Id, Width, Height, Order int
	Used bool
}

func main() {
	var n, k int
	Scan(&n, &k)
	tops := make([]*Unit, n)
	bodies := make([]*Unit, n*2)
	bottoms := make([]*Unit, n)
	for i := range tops {
		u := new(Unit)
		u.Id = i+1
		Scan(&u.Width)
		tops[i] = u
	}
	for _, u := range tops {
		Scan(&u.Height)
	}
	for i := range bodies {
		u := new(Unit)
		u.Id = i+1
		Scan(&u.Width)
		bodies[i] = u
	}
	for _, u := range bodies {
		Scan(&u.Height)
	}
	for i := range bottoms {
		u := new(Unit)
		u.Id = i+1
		Scan(&u.Width)
		bottoms[i] = u
	}
	for _, u := range bottoms {
		Scan(&u.Height)
	}
	sort.Slice(tops, func(i, j int) bool {
		return tops[i].Width < tops[j].Width
	})
	sort.Slice(bodies, func(i, j int) bool {
		return bodies[i].Width < bodies[j].Width
	})
	sort.Slice(bottoms, func(i, j int) bool {
		return bottoms[i].Width < bottoms[j].Width
	})
	for i, u := range tops {
		u.Order = i
	}
	for i, u := range bodies {
		u.Order = i
	}
	for i, u := range bottoms {
		u.Order = i
	}
	sets := make([][]*Unit, 0, k)
	for _, btm := range bottoms[:k] {
		var top *Unit
		for len(tops) > 0 && top == nil {
			if tops[0].Width > btm.Width {
				top = tops[0]
			}
			tops = tops[1:]
		}
		body := make([]*Unit, 0, 2)
		for len(bodies) > 0 && len(body) < 2 {
			tmp := bodies[0]
			if top.Width < tmp.Width {
				body = append(body, tmp)
			}
			bodies = bodies[1:]
		}
		sets = append(sets, []*Unit{top, body[0], body[1], btm})
	}
	for _, us := range sets {
		for _, u := range us {
			u.Used = true
		}
	}
	sort.Slice(sets, func(i, j int) bool {
		return sum(sets[i]) > sum(sets[j])
	})
	hmin, hmax := sum(sets[k-1]), sum(sets[0])
	hhalf := (hmax+hmin)/2
	for i := 0; i < k; i++ {
		for j := k-1; j >= 0; j-- {
			if i==j {
				continue
			}
			if sets[i][2].Width < sets[j][0].Width {
				continue
			}
			if sets[j][2].Width < sets[j][0].Width {
				continue
			}
			h1 := sum(sets[i])
			h2 := sum(sets[j])
			d0 := abs(h1-hhalf)+abs(h2-hhalf)
			sets[i][2], sets[j][2] = sets[j][2], sets[i][2]
			h3 := sum(sets[i])
			h4 := sum(sets[j])
			d1 := abs(h3-hhalf)+abs(h4-hhalf)
			if d1 < d0 {
				break
			} else {
				sets[i][2], sets[j][2] = sets[j][2], sets[i][2]
			}
		}
	}
	for _, s := range sets {
		Println([]any{s[0].Id, s[1].Id, s[2].Id, s[3].Id}...)
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	} else {
		return a
	}
}

func sum(us []*Unit) (h int) {
	for _, u := range us {
		h += u.Height
	}
	return
}
0