main.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. if cumulativeScore >= bestTotalScore {
  199. bestTotalScore = cumulativeScore
  200. // Go back to random depth and make a new change
  201. return rand.Intn(depth), false
  202. } else {
  203. // Go back to fix change that lowered our score
  204. return fromDepth, true
  205. }
  206. }
  207. var rdepth int
  208. var fix bool
  209. r := c.pickBestRide()
  210. if r != nil {
  211. rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, fromDepth)
  212. // if depth == reverse depth, make a change
  213. // and go back up the stack
  214. for depth == rdepth {
  215. var r *Ride
  216. if fix {
  217. r = c.pickBestRide()
  218. } else {
  219. r = c.pickRandomRide()
  220. }
  221. // note fromDepth reset to depth
  222. rdepth, fix = c.AssignRideRecur(r, cumulativeScore, depth, depth)
  223. }
  224. } else {
  225. // another car may still have other rides
  226. rdepth, fix = Choose(cumulativeScore, depth, fromDepth)
  227. }
  228. // add back the one we popped at beginning of function
  229. Sched.Add(c)
  230. // go back down the stack
  231. return rdepth, fix
  232. }
  233. func solve() {
  234. rand.Seed(1)
  235. sort.Sort(ByEndtime(Rides))
  236. Sched = &prioq{}
  237. // create cars
  238. for i := 0; i < F; i++ {
  239. c := &Car{
  240. ID: i,
  241. Arrival: 0,
  242. X: 0,
  243. Y: 0,
  244. }
  245. Cars = append(Cars, c)
  246. Sched.Add(c)
  247. }
  248. // start recursion
  249. Choose(0, 0, 0)
  250. }
  251. func main() {
  252. sample := os.Args[1]
  253. fileIn := sample + ".in"
  254. fileOut := sample + ".out"
  255. var err error
  256. input, err = os.Open(fileIn)
  257. if err != nil {
  258. panic(fmt.Sprintf("open %s: %v", fileIn, err))
  259. }
  260. output, err = os.Create(fileOut)
  261. if err != nil {
  262. panic(fmt.Sprintf("creating %s: %v", fileOut, err))
  263. }
  264. defer input.Close()
  265. defer output.Close()
  266. // Global
  267. R = readInt()
  268. C = readInt()
  269. F = readInt()
  270. N = readInt()
  271. B = readInt()
  272. T = readInt()
  273. for i := 0; i < N; i++ {
  274. Rides = append(Rides, &Ride{
  275. ID: i,
  276. a: readInt(),
  277. b: readInt(),
  278. x: readInt(),
  279. y: readInt(),
  280. s: readInt(),
  281. f: readInt(),
  282. })
  283. }
  284. solve()
  285. }
  286. func readInt() int {
  287. var i int
  288. fmt.Fscanf(input, "%d", &i)
  289. return i
  290. }
  291. func readString() string {
  292. var str string
  293. fmt.Fscanf(input, "%s", &str)
  294. return str
  295. }
  296. func readFloat() float64 {
  297. var x float64
  298. fmt.Fscanf(input, "%f", &x)
  299. return x
  300. }
  301. // Prioq
  302. // Invariant: both children are bigger
  303. type prioq struct {
  304. bintree []*Car
  305. }
  306. func (c *Car) greater(c2 *Car) bool {
  307. if c.Arrival == c2.Arrival {
  308. return c.ID > c2.ID
  309. }
  310. return c.Arrival > c2.Arrival
  311. }
  312. func (pq *prioq) Add(car *Car) {
  313. pq.bintree = append(pq.bintree, car)
  314. pq.bintree[len(pq.bintree)-1].pqindex = len(pq.bintree) - 1
  315. // Rebalance tree to respect invariant
  316. var i = len(pq.bintree) - 1
  317. var p = (i - 1) / 2
  318. for p >= 0 && pq.bintree[p].greater(pq.bintree[i]) {
  319. pq.bintree[p], pq.bintree[i] = pq.bintree[i], pq.bintree[p]
  320. pq.bintree[p].pqindex = p
  321. pq.bintree[i].pqindex = i
  322. i = p
  323. p = (i - 1) / 2
  324. }
  325. }
  326. func (pq *prioq) Pop() *Car {
  327. if len(pq.bintree) == 0 {
  328. return nil
  329. }
  330. if len(pq.bintree) == 1 {
  331. elem := pq.bintree[0]
  332. pq.bintree = pq.bintree[:0]
  333. return elem
  334. }
  335. pq.bintree[0].pqindex = -1
  336. elem := pq.bintree[0]
  337. // Put last element at root
  338. pq.bintree[0] = pq.bintree[len(pq.bintree)-1]
  339. pq.bintree[0].pqindex = 0
  340. // Remove last element
  341. pq.bintree = pq.bintree[:len(pq.bintree)-1]
  342. // 1 9
  343. // 10 9 10 12
  344. // 11 12 13 14 -> 11 12 13 14
  345. // 12
  346. // Rebalance tree to respect invariant
  347. len := len(pq.bintree)
  348. i, left, right := 0, 0, 0
  349. for {
  350. left = 2*i + 1
  351. right = 2*i + 2
  352. if left < len && right < len { // Two children
  353. if !pq.bintree[left].greater(pq.bintree[right]) {
  354. if !pq.bintree[i].greater(pq.bintree[left]) {
  355. break // Inferior to both children
  356. } else {
  357. pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
  358. pq.bintree[i].pqindex = i
  359. pq.bintree[left].pqindex = left
  360. i = left
  361. }
  362. } else {
  363. if !pq.bintree[i].greater(pq.bintree[right]) {
  364. break // Inferior to both children
  365. } else {
  366. pq.bintree[i], pq.bintree[right] = pq.bintree[right], pq.bintree[i]
  367. pq.bintree[i].pqindex = i
  368. pq.bintree[right].pqindex = right
  369. i = right
  370. }
  371. }
  372. } else if left < len { // One child (left)
  373. if !pq.bintree[i].greater(pq.bintree[left]) {
  374. break // Inferior to only child
  375. }
  376. pq.bintree[i], pq.bintree[left] = pq.bintree[left], pq.bintree[i]
  377. pq.bintree[i].pqindex = i
  378. pq.bintree[left].pqindex = left
  379. i = left
  380. } else { // No child
  381. break
  382. }
  383. }
  384. return elem
  385. }
  386. func (pq *prioq) RemoveAtIndex(k int) *Car {
  387. if k == 0 {
  388. return pq.Pop()
  389. }
  390. pq.bintree[k].pqindex = -1
  391. elem := pq.bintree[k]
  392. reassign := pq.bintree[k+1:]
  393. pq.bintree = pq.bintree[:k]
  394. for _, c := range reassign {
  395. pq.Add(c)
  396. }
  397. return elem
  398. }
  399. func (pq *prioq) empty() bool {
  400. return len(pq.bintree) == 0
  401. }