main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "bufio"
  4. "container/heap"
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // Item ...
  12. type Item struct {
  13. time time.Time
  14. depart int
  15. }
  16. // Schedule ...
  17. type Schedule []Item
  18. func (s Schedule) Len() int {
  19. return len(s)
  20. }
  21. func (s Schedule) Less(i, j int) bool {
  22. if s[j].time.Equal(s[i].time) { // Very important!
  23. return s[i].depart < s[j].depart
  24. }
  25. return s[j].time.After(s[i].time)
  26. }
  27. func (s Schedule) Swap(i, j int) {
  28. s[i], s[j] = s[j], s[i]
  29. }
  30. // Push ...
  31. func (s *Schedule) Push(val interface{}) {
  32. *s = append(*s, val.(Item))
  33. }
  34. // Pop ...
  35. func (s *Schedule) Pop() interface{} {
  36. n := s.Len()
  37. top := (*s)[n-1]
  38. *s = (*s)[:n-1]
  39. return top
  40. }
  41. func read2Time(s *bufio.Scanner) (time.Time, time.Time) {
  42. s.Scan()
  43. t := strings.Split(s.Text(), " ")
  44. t1, _ := time.Parse("15:04", t[0])
  45. t2, _ := time.Parse("15:04", t[1])
  46. return t1, t2
  47. }
  48. func readInt(s *bufio.Scanner) int {
  49. s.Scan()
  50. i, _ := strconv.ParseInt(s.Text(), 10, 32)
  51. return int(i)
  52. }
  53. func read2Int(s *bufio.Scanner) (int, int) {
  54. s.Scan()
  55. n := strings.Split(s.Text(), " ")
  56. n1, _ := strconv.ParseInt(n[0], 10, 32)
  57. n2, _ := strconv.ParseInt(n[1], 10, 32)
  58. return int(n1), int(n2)
  59. }
  60. func readInt64(s *bufio.Scanner) int64 {
  61. s.Scan()
  62. i, _ := strconv.ParseInt(s.Text(), 10, 32)
  63. return i
  64. }
  65. func readStr(s *bufio.Scanner) string {
  66. s.Scan()
  67. return s.Text()
  68. }
  69. func countTrains(scanner *bufio.Scanner) [][]int {
  70. caseCnt := readInt(scanner) // Read case count
  71. answer := make([][]int, caseCnt)
  72. for i := range answer {
  73. answer[i] = make([]int, 2)
  74. }
  75. for i := 0; i < caseCnt; i++ {
  76. var cd = time.Duration(readInt64(scanner)) * time.Minute
  77. cntAB, cntBA := read2Int(scanner)
  78. skdA, skdB := &Schedule{}, &Schedule{}
  79. for j := 0; j < cntAB; j++ {
  80. tOutA, tInB := read2Time(scanner)
  81. tInB = tInB.Add(cd)
  82. heap.Push(skdA, Item{tOutA, 1})
  83. heap.Push(skdB, Item{tInB, -1})
  84. }
  85. for j := 0; j < cntBA; j++ {
  86. tOutB, tInA := read2Time(scanner)
  87. tInA = tInA.Add(cd)
  88. heap.Push(skdA, Item{tInA, -1})
  89. heap.Push(skdB, Item{tOutB, 1})
  90. }
  91. currCntA, currCntB := 0, 0
  92. for skdA.Len() != 0 {
  93. top := heap.Pop(skdA)
  94. currCntA += top.(Item).depart
  95. if currCntA > answer[i][0] {
  96. answer[i][0] = currCntA
  97. }
  98. }
  99. for skdB.Len() != 0 {
  100. top := heap.Pop(skdB)
  101. currCntB += top.(Item).depart
  102. if currCntB > answer[i][1] {
  103. answer[i][1] = currCntB
  104. }
  105. }
  106. }
  107. return answer
  108. }
  109. func main() {
  110. inputFiles := []string{"B-small-practice.in", "B-large-practice.in", "test.in"}
  111. outputFiles := []string{"result-small.out", "result-large.out", "test.out"}
  112. const (
  113. small = iota
  114. large
  115. test
  116. )
  117. fileType := test
  118. fin, _ := os.Open(inputFiles[fileType])
  119. defer fin.Close()
  120. scanner := bufio.NewScanner(fin)
  121. answer := countTrains(scanner)
  122. fout, _ := os.Create(outputFiles[fileType])
  123. defer fout.Close()
  124. for i, v := range answer {
  125. s := fmt.Sprintf("Case #%d: %d %d\n", i+1, v[0], v[1])
  126. fout.WriteString(s)
  127. }
  128. }