main.go 9.2 KB

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