main.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "os"
  6. "sort"
  7. )
  8. var input *os.File
  9. var output *os.File
  10. var R int
  11. var C int
  12. var F int
  13. var N int
  14. var B int
  15. var T int
  16. var Rides []*Ride
  17. var Cars []*Car
  18. var Sched Scheduler
  19. type Ride struct {
  20. ID int
  21. a, b, x, y, s, f int
  22. used bool
  23. }
  24. func (r Ride) Length() int {
  25. xdist := r.a - r.x
  26. if xdist < 0 {
  27. xdist = -xdist
  28. }
  29. ydist := r.b - r.y
  30. if ydist < 0 {
  31. ydist = -ydist
  32. }
  33. return xdist + ydist
  34. }
  35. func abs(x int) int {
  36. if x < 0 {
  37. return -x
  38. } else {
  39. return x
  40. }
  41. }
  42. func (r *Ride) length() int {
  43. return abs(r.a-r.x) + abs(r.b-r.y)
  44. }
  45. type ByEndtime []*Ride
  46. func (rs ByEndtime) Len() int { return len(rs) }
  47. func (rs ByEndtime) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
  48. func (rs ByEndtime) Less(i, j int) bool {
  49. return rs[i].f < rs[j].f || (rs[i].f == rs[j].f && rs[i].length() < rs[j].length())
  50. }
  51. type Scheduler interface {
  52. Add(*Car)
  53. RemoveAtIndex(k int) *Car
  54. Pop() *Car
  55. }
  56. type Car struct {
  57. ID int
  58. Rides []int
  59. Arrival int
  60. X int
  61. Y int
  62. score int
  63. pqindex int // used for removal in prority queue
  64. }
  65. func (c *Car) Update(r *Ride) {
  66. c.moveTo(r.a, r.b)
  67. if c.Arrival <= r.s {
  68. c.Arrival = r.s
  69. // count bonus points for being on time
  70. c.score += B
  71. }
  72. c.moveTo(r.x, r.y)
  73. c.score += r.length()
  74. c.Rides = append(c.Rides, r.ID)
  75. }
  76. func (c *Car) EarliestFinish(r *Ride) int {
  77. copy := &Car{
  78. Arrival: c.Arrival,
  79. X: c.X,
  80. Y: c.Y,
  81. }
  82. copy.moveTo(r.a, r.b)
  83. if copy.Arrival < r.s {
  84. copy.Arrival = r.s
  85. }
  86. copy.moveTo(r.x, r.y)
  87. return copy.Arrival
  88. }
  89. func (c *Car) moveTo(x, y int) {
  90. xdist := c.X - x
  91. if xdist < 0 {
  92. xdist = -xdist
  93. }
  94. ydist := c.Y - y
  95. if ydist < 0 {
  96. ydist = -ydist
  97. }
  98. c.Arrival += xdist + ydist
  99. c.X = x
  100. c.Y = y
  101. }
  102. func (c *Car) distanceTo(x, y int) int {
  103. return abs(c.X-x) + abs(c.Y-y)
  104. }
  105. func max(a, b int) int {
  106. if a > b {
  107. return a
  108. } else {
  109. return b
  110. }
  111. }
  112. var bestTotalScore int
  113. // invariant : when entering and leaving function,
  114. // Sched has the "same" heap. Same means successive
  115. // popped values will be the same (but the internal
  116. // ordering of nodes may be different)
  117. func Choose(cumulativeScore int, depth int) {
  118. if cumulativeScore > bestTotalScore {
  119. bestTotalScore = cumulativeScore
  120. }
  121. c := Sched.Pop()
  122. if c == nil {
  123. // Stop recursion, empty car list,
  124. // could not match cars to rides
  125. return
  126. }
  127. var bestRides []struct {
  128. r *Ride
  129. lenOfRide int
  130. total int
  131. }
  132. // fmt.Printf("car %d\n", c.ID)
  133. for _, r := range Rides {
  134. if r.used {
  135. continue
  136. }
  137. if r.Length() > 6000 {
  138. continue
  139. }
  140. if r.f < c.EarliestFinish(r) {
  141. continue
  142. }
  143. // fmt.Printf("%d %d -> %d %d\n", r.a, r.b, r.x, r.y)
  144. lenOfRide := r.length()
  145. total := max(c.distanceTo(r.a, r.b), r.s-c.Arrival) + lenOfRide
  146. // fmt.Printf("%d/%d\n", lenOfRide, total)
  147. if len(bestRides) == 0 || lenOfRide*bestRides[len(bestRides)-1].total > total*bestRides[len(bestRides)-1].lenOfRide {
  148. bestRides = append(bestRides, struct {
  149. r *Ride
  150. lenOfRide int
  151. total int
  152. }{r, lenOfRide, total})
  153. // shitty sort-of-correct-but-quite-incorrect way
  154. // of picking next best n rides
  155. n := 1
  156. if rand.Intn(max(1, int(N/5))) == 0 {
  157. n = 3
  158. }
  159. if len(bestRides) > n {
  160. bestRides = bestRides[len(bestRides)-n:]
  161. }
  162. }
  163. }
  164. // if bestRide != nil {
  165. // fmt.Printf("Picking %d %d -> %d %d\n", bestRide.a, bestRide.b, bestRide.x, bestRide.y)
  166. // }
  167. if len(bestRides) != 0 {
  168. for _, br := range bestRides {
  169. r := br.r
  170. r.used = true
  171. oldC := *c
  172. c.Update(r)
  173. Sched.Add(c)
  174. // recursion 101
  175. Choose(cumulativeScore+c.score-oldC.score, depth+1)
  176. Sched.RemoveAtIndex(c.pqindex)
  177. // pqindex is meaningless now but it's ok
  178. // it will be fixed when c is added again
  179. *c = oldC
  180. r.used = false
  181. }
  182. } else {
  183. // another car may still have other rides
  184. Choose(cumulativeScore, depth)
  185. }
  186. // add back the one we popped at beginning of function
  187. Sched.Add(c)
  188. }
  189. func solve() {
  190. rand.Seed(1)
  191. sort.Sort(ByEndtime(Rides))
  192. Sched = &prioq{}
  193. // create cars
  194. for i := 0; i < F; i++ {
  195. c := &Car{
  196. ID: i,
  197. Arrival: 0,
  198. X: 0,
  199. Y: 0,
  200. }
  201. Cars = append(Cars, c)
  202. Sched.Add(c)
  203. }
  204. // start recursion
  205. Choose(0, 0)
  206. for _, c := range Cars {
  207. fmt.Fprintf(output, "%d", len(c.Rides))
  208. for _, ri := range c.Rides {
  209. fmt.Fprintf(output, " %d", ri)
  210. }
  211. fmt.Fprintf(output, "\n")
  212. }
  213. fmt.Printf("%d\n", bestTotalScore)
  214. }
  215. func main() {
  216. sample := os.Args[1]
  217. fileIn := sample + ".in"
  218. fileOut := sample + ".out"
  219. var err error
  220. input, err = os.Open(fileIn)
  221. if err != nil {
  222. panic(fmt.Sprintf("open %s: %v", fileIn, err))
  223. }
  224. output, err = os.Create(fileOut)
  225. if err != nil {
  226. panic(fmt.Sprintf("creating %s: %v", fileOut, err))
  227. }
  228. defer input.Close()
  229. defer output.Close()
  230. // Global
  231. R = readInt()
  232. C = readInt()
  233. F = readInt()
  234. N = readInt()
  235. B = readInt()
  236. T = readInt()
  237. for i := 0; i < N; i++ {
  238. Rides = append(Rides, &Ride{
  239. ID: i,
  240. a: readInt(),
  241. b: readInt(),
  242. x: readInt(),
  243. y: readInt(),
  244. s: readInt(),
  245. f: readInt(),
  246. })
  247. }
  248. solve()
  249. }
  250. func readInt() int {
  251. var i int
  252. fmt.Fscanf(input, "%d", &i)
  253. return i
  254. }
  255. func readString() string {
  256. var str string
  257. fmt.Fscanf(input, "%s", &str)
  258. return str
  259. }
  260. func readFloat() float64 {
  261. var x float64
  262. fmt.Fscanf(input, "%f", &x)
  263. return x
  264. }
  265. // Prioq
  266. // Invariant: both children are bigger
  267. type prioq struct {
  268. bintree []*Car
  269. }
  270. func (pq *prioq) Add(car *Car) {
  271. pq.bintree = append(pq.bintree, car)
  272. pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
  273. // Rebalance tree to respect invariant
  274. var i = len(pq.bintree) - 1
  275. var p = (i - 1) / 2
  276. for p >= 0 && pq.bintree[p].Arrival > pq.bintree[i].Arrival {
  277. pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
  278. pq.bintree[p].pqindex = p
  279. pq.bintree[i].pqindex = i
  280. i = p
  281. p = (i - 1) / 2
  282. }
  283. }
  284. func (pq *prioq) RemoveAtIndex(k int) *Car {
  285. if len(pq.bintree) == 0 {
  286. return nil
  287. }
  288. if k == len(pq.bintree)-1 {
  289. elem := pq.bintree[k]
  290. pq.bintree = pq.bintree[:k]
  291. return elem
  292. }
  293. pq.bintree[k].pqindex = -1
  294. elem := pq.bintree[k]
  295. // Put last element at hole
  296. pq.bintree[k] = pq.bintree[len(pq.bintree)-1]
  297. pq.bintree[k].pqindex = k
  298. // Remove last element
  299. pq.bintree = pq.bintree[:len(pq.bintree)-1]
  300. // 1 9
  301. // 10 9 10 12
  302. // 11 12 13 14 -> 11 12 13 14
  303. // 12
  304. // Rebalance tree to respect invariant
  305. len := len(pq.bintree)
  306. i := k
  307. left, right := 0, 0
  308. for {
  309. left = 2*i + 1
  310. right = 2*i + 2
  311. if left < len && right < len { // Two children
  312. if pq.bintree[left].Arrival <= pq.bintree[right].Arrival {
  313. if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
  314. break // Inferior to both children
  315. } else {
  316. pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
  317. pq.bintree[i].pqindex = i
  318. pq.bintree[left].pqindex = left
  319. i = left
  320. }
  321. } else {
  322. if pq.bintree[i].Arrival <= pq.bintree[right].Arrival {
  323. break // Inferior to both children
  324. } else {
  325. pq.bintree[i], pq.bintree[right] = pq.bintree[right], pq.bintree[i]
  326. pq.bintree[i].pqindex = i
  327. pq.bintree[right].pqindex = right
  328. i = right
  329. }
  330. }
  331. } else if left < len { // One child (left)
  332. if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
  333. break // Inferior to only child
  334. }
  335. pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
  336. pq.bintree[i].pqindex = i
  337. pq.bintree[left].pqindex = left
  338. i = left
  339. } else { // No child
  340. break
  341. }
  342. }
  343. return elem
  344. }
  345. func (pq *prioq) Pop() *Car {
  346. return pq.RemoveAtIndex(0)
  347. }
  348. func (pq *prioq) empty() bool {
  349. return len(pq.bintree) == 0
  350. }