main.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  2. import (
  3. "container/heap"
  4. "fmt"
  5. )
  6. func main() {
  7. var T, N, K int
  8. fmt.Scan(&T)
  9. for cid := 1; cid <= T; cid++ { // Too slow for large dataset
  10. println("Case", cid)
  11. fmt.Printf("Case #%d: ", cid)
  12. fmt.Scan(&N)
  13. fmt.Scan(&K)
  14. if K == N {
  15. fmt.Printf("0 0\n")
  16. continue
  17. } else if K == 1 {
  18. fmt.Printf("%d %d\n", N/2, N-N/2-1)
  19. continue
  20. }
  21. var pq PQ
  22. heap.Push(&pq, N/2)
  23. heap.Push(&pq, N-N/2-1)
  24. for i := 1; i < K-1; i++ {
  25. stall := heap.Pop(&pq).(int)
  26. heap.Push(&pq, stall/2)
  27. heap.Push(&pq, stall-stall/2-1)
  28. }
  29. stall := heap.Pop(&pq).(int)
  30. fmt.Printf("%d %d\n", stall/2, stall-stall/2-1)
  31. }
  32. }
  33. // PQ ...
  34. type PQ struct {
  35. queue []int
  36. length int
  37. }
  38. func (pq PQ) Len() int { return pq.length }
  39. func (pq PQ) Less(i, j int) bool { return pq.queue[j] < pq.queue[i] }
  40. func (pq PQ) Swap(i, j int) { pq.queue[i], pq.queue[j] = pq.queue[j], pq.queue[i] }
  41. // Push ...
  42. func (pq *PQ) Push(x interface{}) {
  43. pq.queue = append(pq.queue, x.(int))
  44. pq.length++
  45. }
  46. // Pop ...
  47. func (pq *PQ) Pop() interface{} {
  48. pq.length--
  49. top := pq.queue[pq.length]
  50. pq.queue = pq.queue[:pq.length]
  51. return top
  52. }