main.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. func save() {
  114. if bestTotalScore < 10900000 {
  115. // TODO find a better way to avoid too many writes
  116. return
  117. }
  118. output.Truncate(0)
  119. output.Seek(0, 0)
  120. for _, c := range Cars {
  121. fmt.Fprintf(output, "%d", len(c.Rides))
  122. for _, ri := range c.Rides {
  123. fmt.Fprintf(output, " %d", ri)
  124. }
  125. fmt.Fprintf(output, "\n")
  126. }
  127. fmt.Printf("%d\n", bestTotalScore)
  128. }
  129. // invariant : when entering and leaving function,
  130. // Sched has the "same" heap. Same means successive
  131. // popped values will be the same (but the internal
  132. // ordering of nodes may be different)
  133. func Choose(cumulativeScore int, depth int) {
  134. if cumulativeScore > bestTotalScore {
  135. bestTotalScore = cumulativeScore
  136. save()
  137. }
  138. c := Sched.Pop()
  139. if c == nil {
  140. // Stop recursion, empty car list,
  141. // could not match cars to rides
  142. return
  143. }
  144. var bestRides []struct {
  145. r *Ride
  146. lenOfRide int
  147. total int
  148. }
  149. // fmt.Printf("car %d\n", c.ID)
  150. for _, r := range Rides {
  151. if r.used {
  152. continue
  153. }
  154. if r.Length() > 8000 {
  155. continue
  156. }
  157. if r.f < c.EarliestFinish(r) {
  158. continue
  159. }
  160. // fmt.Printf("%d %d -> %d %d\n", r.a, r.b, r.x, r.y)
  161. lenOfRide := r.length()
  162. total := max(c.distanceTo(r.a, r.b), r.s-c.Arrival) + lenOfRide
  163. // fmt.Printf("%d/%d\n", lenOfRide, total)
  164. if len(bestRides) == 0 || lenOfRide*bestRides[len(bestRides)-1].total > total*bestRides[len(bestRides)-1].lenOfRide {
  165. bestRides = append(bestRides, struct {
  166. r *Ride
  167. lenOfRide int
  168. total int
  169. }{r, lenOfRide, total})
  170. // shitty sort-of-correct-but-quite-incorrect way
  171. // of picking next best n rides
  172. n := 1
  173. if rand.Intn(max(1, int(N/20))) == 0 {
  174. n = 2
  175. }
  176. if len(bestRides) > n {
  177. bestRides = bestRides[len(bestRides)-n:]
  178. }
  179. }
  180. }
  181. // if bestRide != nil {
  182. // fmt.Printf("Picking %d %d -> %d %d\n", bestRide.a, bestRide.b, bestRide.x, bestRide.y)
  183. // }
  184. if len(bestRides) != 0 {
  185. if (len(bestRides)) >= 2 {
  186. // if big difference in length, try both
  187. // otherwise pick best according to score
  188. d := abs(bestRides[0].r.length() - bestRides[1].r.length())
  189. if d < 1000 {
  190. bestRides = bestRides[1:]
  191. }
  192. }
  193. for _, br := range bestRides {
  194. r := br.r
  195. r.used = true
  196. oldC := *c
  197. c.Update(r)
  198. Sched.Add(c)
  199. // recursion 101
  200. Choose(cumulativeScore+c.score-oldC.score, depth+1)
  201. Sched.RemoveAtIndex(c.pqindex)
  202. // pqindex is meaningless now but it's ok
  203. // it will be fixed when c is added again
  204. *c = oldC
  205. r.used = false
  206. }
  207. } else {
  208. // another car may still have other rides
  209. Choose(cumulativeScore, depth)
  210. }
  211. // add back the one we popped at beginning of function
  212. Sched.Add(c)
  213. }
  214. func solve() {
  215. rand.Seed(1)
  216. sort.Sort(ByEndtime(Rides))
  217. Sched = &prioq{}
  218. // create cars
  219. for i := 0; i < F; i++ {
  220. c := &Car{
  221. ID: i,
  222. Arrival: 0,
  223. X: 0,
  224. Y: 0,
  225. }
  226. Cars = append(Cars, c)
  227. Sched.Add(c)
  228. }
  229. // start recursion
  230. Choose(0, 0)
  231. }
  232. func main() {
  233. sample := os.Args[1]
  234. fileIn := sample + ".in"
  235. fileOut := sample + ".out"
  236. var err error
  237. input, err = os.Open(fileIn)
  238. if err != nil {
  239. panic(fmt.Sprintf("open %s: %v", fileIn, err))
  240. }
  241. output, err = os.Create(fileOut)
  242. if err != nil {
  243. panic(fmt.Sprintf("creating %s: %v", fileOut, err))
  244. }
  245. defer input.Close()
  246. defer output.Close()
  247. // Global
  248. R = readInt()
  249. C = readInt()
  250. F = readInt()
  251. N = readInt()
  252. B = readInt()
  253. T = readInt()
  254. for i := 0; i < N; i++ {
  255. Rides = append(Rides, &Ride{
  256. ID: i,
  257. a: readInt(),
  258. b: readInt(),
  259. x: readInt(),
  260. y: readInt(),
  261. s: readInt(),
  262. f: readInt(),
  263. })
  264. }
  265. solve()
  266. }
  267. func readInt() int {
  268. var i int
  269. fmt.Fscanf(input, "%d", &i)
  270. return i
  271. }
  272. func readString() string {
  273. var str string
  274. fmt.Fscanf(input, "%s", &str)
  275. return str
  276. }
  277. func readFloat() float64 {
  278. var x float64
  279. fmt.Fscanf(input, "%f", &x)
  280. return x
  281. }
  282. // Prioq
  283. // Invariant: both children are bigger
  284. type prioq struct {
  285. bintree []*Car
  286. }
  287. func (pq *prioq) Add(car *Car) {
  288. pq.bintree = append(pq.bintree, car)
  289. pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
  290. // Rebalance tree to respect invariant
  291. var i = len(pq.bintree) - 1
  292. var p = (i - 1) / 2
  293. for p >= 0 && pq.bintree[p].Arrival > pq.bintree[i].Arrival {
  294. pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
  295. pq.bintree[p].pqindex = p
  296. pq.bintree[i].pqindex = i
  297. i = p
  298. p = (i - 1) / 2
  299. }
  300. }
  301. func (pq *prioq) RemoveAtIndex(k int) *Car {
  302. if len(pq.bintree) == 0 {
  303. return nil
  304. }
  305. if k == len(pq.bintree)-1 {
  306. elem := pq.bintree[k]
  307. pq.bintree = pq.bintree[:k]
  308. return elem
  309. }
  310. pq.bintree[k].pqindex = -1
  311. elem := pq.bintree[k]
  312. // Put last element at hole
  313. pq.bintree[k] = pq.bintree[len(pq.bintree)-1]
  314. pq.bintree[k].pqindex = k
  315. // Remove last element
  316. pq.bintree = pq.bintree[:len(pq.bintree)-1]
  317. // 1 9
  318. // 10 9 10 12
  319. // 11 12 13 14 -> 11 12 13 14
  320. // 12
  321. // Rebalance tree to respect invariant
  322. len := len(pq.bintree)
  323. i := k
  324. left, right := 0, 0
  325. for {
  326. left = 2*i + 1
  327. right = 2*i + 2
  328. if left < len && right < len { // Two children
  329. if pq.bintree[left].Arrival <= pq.bintree[right].Arrival {
  330. if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
  331. break // Inferior to both children
  332. } else {
  333. pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
  334. pq.bintree[i].pqindex = i
  335. pq.bintree[left].pqindex = left
  336. i = left
  337. }
  338. } else {
  339. if pq.bintree[i].Arrival <= pq.bintree[right].Arrival {
  340. break // Inferior to both children
  341. } else {
  342. pq.bintree[i], pq.bintree[right] = pq.bintree[right], pq.bintree[i]
  343. pq.bintree[i].pqindex = i
  344. pq.bintree[right].pqindex = right
  345. i = right
  346. }
  347. }
  348. } else if left < len { // One child (left)
  349. if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
  350. break // Inferior to only child
  351. }
  352. pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
  353. pq.bintree[i].pqindex = i
  354. pq.bintree[left].pqindex = left
  355. i = left
  356. } else { // No child
  357. break
  358. }
  359. }
  360. return elem
  361. }
  362. func (pq *prioq) Pop() *Car {
  363. return pq.RemoveAtIndex(0)
  364. }
  365. func (pq *prioq) empty() bool {
  366. return len(pq.bintree) == 0
  367. }