main.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. func (c *Car) AssignRideRecur(r *Ride, cumulativeScore int, depth int, fromDepth int) (int, bool) {
  130. r.used = true
  131. oldC := *c
  132. c.Update(r)
  133. Sched.Add(c)
  134. // recursion 101
  135. rdepth, fix := Choose(cumulativeScore+c.score-oldC.score, depth+1, fromDepth)
  136. Sched.RemoveAtIndex(c.pqindex)
  137. // pqindex is meaningless now but it's ok
  138. // it will be fixed when c is added again
  139. *c = oldC
  140. r.used = false
  141. return rdepth, fix
  142. }
  143. func (c *Car) CanPick(r *Ride) bool {
  144. return !r.used && r.Length() <= 8000 && r.f >= c.EarliestFinish(r)
  145. }
  146. func (c *Car) pickBestRide() *Ride {
  147. var bestRide *Ride
  148. var bestRideTotal int
  149. var bestRideLen int
  150. for _, r := range Rides {
  151. if !c.CanPick(r) {
  152. continue
  153. }
  154. lenOfRide := r.length()
  155. total := max(c.distanceTo(r.a, r.b), r.s-c.Arrival) + lenOfRide
  156. if bestRide == nil || lenOfRide*bestRideTotal > total*bestRideLen {
  157. bestRide = r
  158. bestRideLen = lenOfRide
  159. bestRideTotal = total
  160. }
  161. }
  162. return bestRide
  163. }
  164. func (c *Car) pickRandomRide() *Ride {
  165. count := 0
  166. for _, r := range Rides {
  167. if !c.CanPick(r) {
  168. continue
  169. }
  170. count++
  171. }
  172. i := rand.Intn(count)
  173. for _, r := range Rides {
  174. if !c.CanPick(r) {
  175. continue
  176. }
  177. if i == 0 {
  178. return r
  179. }
  180. i = i - 1
  181. }
  182. return nil
  183. }
  184. // invariant : when entering and leaving function,
  185. // Sched has the "same" heap. Same means successive
  186. // popped values will be the same (but the internal
  187. // ordering of nodes may be different)
  188. // Return value (a,b) :
  189. // - rdepth >= 0 return back to that depth
  190. // - rdepth < 0 completely unwind stack and finish
  191. // - fix == false try to make a change at depth `rdepth'
  192. // - fix == true change back, use "optimal" ride again
  193. func Choose(cumulativeScore int, depth int, fromDepth int) (int, bool) {
  194. c := Sched.Pop()
  195. if c == nil {
  196. // At this point we have a complete configuration
  197. fmt.Printf("score obtained: %d depth: %d\n", cumulativeScore, depth)
  198. // INVESTIGATE HERE : why do we have different scores ???
  199. // when we're only asking to fix 22 down in the stack
  200. return 8270, true
  201. if cumulativeScore >= bestTotalScore {
  202. bestTotalScore = cumulativeScore
  203. // Go back to random depth and make a new change
  204. return rand.Intn(depth), false
  205. } else {
  206. // Go back to fix change that lowered our score
  207. return fromDepth, true
  208. }
  209. }
  210. var rdepth int
  211. var fix bool
  212. r := c.pickBestRide()
  213. // if depth >= 252 && depth <= 258 {
  214. // fmt.Printf("yo picking id %d score %d c=%+v\n", r.ID, cumulativeScore, c)
  215. // }
  216. if r != nil {
  217. rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, fromDepth)
  218. // if depth == reverse depth, make a change
  219. // and go back up the stack
  220. for depth == rdepth {
  221. var r *Ride
  222. if fix {
  223. r = c.pickBestRide()
  224. fmt.Printf("pickBestRide id %d depth %d\n", r.ID, depth)
  225. } else {
  226. r = c.pickRandomRide()
  227. fmt.Printf("pickRandomRide id %d depth %d\n", r.ID, depth)
  228. }
  229. // note fromDepth reset to depth
  230. rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, depth)
  231. }
  232. } else {
  233. // another car may still have other rides
  234. rdepth, fix = Choose(cumulativeScore, depth, fromDepth)
  235. }
  236. // add back the one we popped at beginning of function
  237. Sched.Add(c)
  238. // go back down the stack
  239. return rdepth, fix
  240. }
  241. func solve() {
  242. rand.Seed(1)
  243. sort.Sort(ByEndtime(Rides))
  244. Sched = &prioq{}
  245. // create cars
  246. for i := 0; i < F; i++ {
  247. c := &Car{
  248. ID: i,
  249. Arrival: 0,
  250. X: 0,
  251. Y: 0,
  252. }
  253. Cars = append(Cars, c)
  254. Sched.Add(c)
  255. }
  256. // start recursion
  257. Choose(0, 0, 0)
  258. }
  259. func main() {
  260. sample := os.Args[1]
  261. fileIn := sample + ".in"
  262. fileOut := sample + ".out"
  263. var err error
  264. input, err = os.Open(fileIn)
  265. if err != nil {
  266. panic(fmt.Sprintf("open %s: %v", fileIn, err))
  267. }
  268. output, err = os.Create(fileOut)
  269. if err != nil {
  270. panic(fmt.Sprintf("creating %s: %v", fileOut, err))
  271. }
  272. defer input.Close()
  273. defer output.Close()
  274. // Global
  275. R = readInt()
  276. C = readInt()
  277. F = readInt()
  278. N = readInt()
  279. B = readInt()
  280. T = readInt()
  281. for i := 0; i < N; i++ {
  282. Rides = append(Rides, &Ride{
  283. ID: i,
  284. a: readInt(),
  285. b: readInt(),
  286. x: readInt(),
  287. y: readInt(),
  288. s: readInt(),
  289. f: readInt(),
  290. })
  291. }
  292. solve()
  293. }
  294. func readInt() int {
  295. var i int
  296. fmt.Fscanf(input, "%d", &i)
  297. return i
  298. }
  299. func readString() string {
  300. var str string
  301. fmt.Fscanf(input, "%s", &str)
  302. return str
  303. }
  304. func readFloat() float64 {
  305. var x float64
  306. fmt.Fscanf(input, "%f", &x)
  307. return x
  308. }
  309. // Prioq
  310. // Invariant: both children are bigger
  311. type prioq struct {
  312. bintree []*Car
  313. }
  314. func (pq *prioq) Add(car *Car) {
  315. pq.bintree = append(pq.bintree, car)
  316. pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
  317. // Rebalance tree to respect invariant
  318. var i = len(pq.bintree) - 1
  319. var p = (i - 1) / 2
  320. for p >= 0 && pq.bintree[p].Arrival > pq.bintree[i].Arrival {
  321. pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
  322. pq.bintree[p].pqindex = p
  323. pq.bintree[i].pqindex = i
  324. i = p
  325. p = (i - 1) / 2
  326. }
  327. }
  328. func (pq *prioq) RemoveAtIndex(k int) *Car {
  329. if len(pq.bintree) == 0 {
  330. return nil
  331. }
  332. if k == len(pq.bintree)-1 {
  333. elem := pq.bintree[k]
  334. pq.bintree = pq.bintree[:k]
  335. return elem
  336. }
  337. pq.bintree[k].pqindex = -1
  338. elem := pq.bintree[k]
  339. // Put last element at hole
  340. pq.bintree[k] = pq.bintree[len(pq.bintree)-1]
  341. pq.bintree[k].pqindex = k
  342. // Remove last element
  343. pq.bintree = pq.bintree[:len(pq.bintree)-1]
  344. // 1 9
  345. // 10 9 10 12
  346. // 11 12 13 14 -> 11 12 13 14
  347. // 12
  348. // Rebalance tree to respect invariant
  349. len := len(pq.bintree)
  350. i := k
  351. left, right := 0, 0
  352. for {
  353. left = 2*i + 1
  354. right = 2*i + 2
  355. if left < len && right < len { // Two children
  356. if pq.bintree[left].Arrival <= pq.bintree[right].Arrival {
  357. if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
  358. break // Inferior to both children
  359. } else {
  360. pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
  361. pq.bintree[i].pqindex = i
  362. pq.bintree[left].pqindex = left
  363. i = left
  364. }
  365. } else {
  366. if pq.bintree[i].Arrival <= pq.bintree[right].Arrival {
  367. break // Inferior to both children
  368. } else {
  369. pq.bintree[i], pq.bintree[right] = pq.bintree[right], pq.bintree[i]
  370. pq.bintree[i].pqindex = i
  371. pq.bintree[right].pqindex = right
  372. i = right
  373. }
  374. }
  375. } else if left < len { // One child (left)
  376. if pq.bintree[i].Arrival <= pq.bintree[left].Arrival {
  377. break // Inferior to only child
  378. }
  379. pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
  380. pq.bintree[i].pqindex = i
  381. pq.bintree[left].pqindex = left
  382. i = left
  383. } else { // No child
  384. break
  385. }
  386. }
  387. return elem
  388. }
  389. func (pq *prioq) Pop() *Car {
  390. return pq.RemoveAtIndex(0)
  391. }
  392. func (pq *prioq) empty() bool {
  393. return len(pq.bintree) == 0
  394. }