1
0
Fork 0
mirror of https://github.com/Luzifer/codingame-solutions.git synced 2024-12-22 18:51:20 +00:00

Add solution for "Roller Coaster"

This commit is contained in:
Knut Ahlers 2016-10-15 22:57:56 +02:00
parent 1ea0dddd6d
commit 178ce63a19
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 79 additions and 1 deletions

View file

@ -7,9 +7,11 @@ Just one thing: **Don't just copy them.** Take the puzzle yourself and afterward
## Finished puzzles ## Finished puzzles
- Medium - Medium
- [Bender - Episode 1](https://www.codingame.com/training/medium/bender-episode-1)
- [Don't Panic - Episode 1](https://www.codingame.com/training/medium/don't-panic-episode-1) - [Don't Panic - Episode 1](https://www.codingame.com/training/medium/don't-panic-episode-1)
- [Shadows of the Knight - Episode 1](https://www.codingame.com/training/medium/shadows-of-the-knight-episode-1) - [Shadows of the Knight - Episode 1](https://www.codingame.com/training/medium/shadows-of-the-knight-episode-1)
- [Bender - Episode 1](https://www.codingame.com/training/medium/bender-episode-1)
- [There is no Spoon - Episode 1](https://www.codingame.com/training/medium/there-is-no-spoon-episode-1) - [There is no Spoon - Episode 1](https://www.codingame.com/training/medium/there-is-no-spoon-episode-1)
- Hard
- [Roller Coaster](https://www.codingame.com/training/hard/roller-coaster)
- Community - Community
- [Texas Holdem](https://www.codingame.com/training/community/texas-holdem) by AdamHill - [Texas Holdem](https://www.codingame.com/training/community/texas-holdem) by AdamHill

View file

@ -0,0 +1,76 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"time"
)
type calculationResult struct{ Seated, Next int }
var (
groups [1000]int
calculationCache = map[int]calculationResult{}
)
func main() {
defer timeTrack(time.Now(), "main")
scanner := bufio.NewScanner(os.Stdin)
scanner.Buffer(make([]byte, 1000000), 1000000)
scanner.Split(bufio.ScanWords)
var L, C, N, i int
scanner.Scan()
L = toInt(scanner.Bytes())
scanner.Scan()
C = toInt(scanner.Bytes())
scanner.Scan()
N = toInt(scanner.Bytes())
log.Printf("C: %d", C)
for ; i < N; i++ {
scanner.Scan()
groups[i] = toInt(scanner.Bytes())
}
var earned, next, seated, j int
for i = 0; i < C; i++ {
key := next
seated = 0
if cr, ok := calculationCache[key]; ok {
seated = cr.Seated
next = cr.Next
} else {
j = 0
for j < N && groups[next] <= L-seated {
seated += groups[next]
j++
next++
if next == N {
next = 0
}
}
calculationCache[key] = calculationResult{Seated: seated, Next: next}
}
earned += seated
}
fmt.Printf("%d\n", earned)
}
func toInt(buf []byte) (n int) {
for _, v := range buf {
n = n*10 + int(v-'0')
}
return
}
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}