Pārlūkot izejas kodu

Check recursive algorithm gives a correct result on scoreboard

Input D scores at 10936033, which is a bit better, but there's still
room for improvement (theoretical max for D is at 14M).
Guillaume Koenig 7 gadi atpakaļ
vecāks
revīzija
b1a7f7bdbf
1 mainītis faili ar 30 papildinājumiem un 12 dzēšanām
  1. 30 12
      main.go

+ 30 - 12
main.go

@@ -137,6 +137,23 @@ func max(a, b int) int {
137 137
 
138 138
 var bestTotalScore int
139 139
 
140
+func save() {
141
+	if bestTotalScore < 10900000 {
142
+		// TODO find a better way to avoid too many writes
143
+		return
144
+	}
145
+	output.Truncate(0)
146
+	output.Seek(0, 0)
147
+	for _, c := range Cars {
148
+		fmt.Fprintf(output, "%d", len(c.Rides))
149
+		for _, ri := range c.Rides {
150
+			fmt.Fprintf(output, " %d", ri)
151
+		}
152
+		fmt.Fprintf(output, "\n")
153
+	}
154
+	fmt.Printf("%d\n", bestTotalScore)
155
+}
156
+
140 157
 // invariant : when entering and leaving function,
141 158
 // Sched has the "same" heap. Same means successive
142 159
 // popped values will be the same (but the internal
@@ -144,6 +161,7 @@ var bestTotalScore int
144 161
 func Choose(cumulativeScore int, depth int) {
145 162
 	if cumulativeScore > bestTotalScore {
146 163
 		bestTotalScore = cumulativeScore
164
+		save()
147 165
 	}
148 166
 	c := Sched.Pop()
149 167
 	if c == nil {
@@ -162,7 +180,7 @@ func Choose(cumulativeScore int, depth int) {
162 180
 		if r.used {
163 181
 			continue
164 182
 		}
165
-		if r.Length() > 6000 {
183
+		if r.Length() > 8000 {
166 184
 			continue
167 185
 		}
168 186
 		if r.f < c.EarliestFinish(r) {
@@ -181,8 +199,8 @@ func Choose(cumulativeScore int, depth int) {
181 199
 			// shitty sort-of-correct-but-quite-incorrect way
182 200
 			// of picking next best n rides
183 201
 			n := 1
184
-			if rand.Intn(max(1, int(N/5))) == 0 {
185
-				n = 3
202
+			if rand.Intn(max(1, int(N/20))) == 0 {
203
+				n = 2
186 204
 			}
187 205
 			if len(bestRides) > n {
188 206
 				bestRides = bestRides[len(bestRides)-n:]
@@ -193,6 +211,15 @@ func Choose(cumulativeScore int, depth int) {
193 211
 	// 	fmt.Printf("Picking %d %d -> %d %d\n", bestRide.a, bestRide.b, bestRide.x, bestRide.y)
194 212
 	// }
195 213
 	if len(bestRides) != 0 {
214
+		if (len(bestRides)) >= 2 {
215
+			// if big difference in length, try both
216
+			// otherwise pick best according to score
217
+			d := abs(bestRides[0].r.length() - bestRides[1].r.length())
218
+			if d < 1000 {
219
+				bestRides = bestRides[1:]
220
+			}
221
+
222
+		}
196 223
 		for _, br := range bestRides {
197 224
 			r := br.r
198 225
 			r.used = true
@@ -236,15 +263,6 @@ func solve() {
236 263
 
237 264
 	// start recursion
238 265
 	Choose(0, 0)
239
-
240
-	for _, c := range Cars {
241
-		fmt.Fprintf(output, "%d", len(c.Rides))
242
-		for _, ri := range c.Rides {
243
-			fmt.Fprintf(output, " %d", ri)
244
-		}
245
-		fmt.Fprintf(output, "\n")
246
-	}
247
-	fmt.Printf("%d\n", bestTotalScore)
248 266
 }
249 267
 
250 268
 func main() {