-
-
Save myeesan/308b619602cd532058be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.PrintWriter | |
import scala.io.Source | |
object SpaceShip { | |
val in = Source.fromFile("sample.in").getLines | |
val out = new PrintWriter("sample.out") | |
case class TestCase(nOfBoosters: Int, constructionTime: Int, nOfStars: Int, distances: List[Int]) { | |
def canBooster(currentTime: Int, seq: List[Int]): Boolean = { | |
//지금 부스터를 써서 만들수 있는시간 보다 남은 시간이 더 큰 | |
seq.exists(p => p <= currentTime) | |
//True | |
} | |
def solve2(nOfBoosters: Int,seq: List[Int],result: Int = 0): Int = { | |
if(seq.isEmpty) { | |
result | |
}else if (nOfBoosters == 0) { | |
result + seq.sum | |
} else if(seq.head + result > constructionTime) { | |
// 부스터를 쓴다. | |
// 현재 부스터를 안쓰고 갈수 있는 시간 | |
// 22 20 부스터를 써서 얻을수 있는 시간은 2 | |
val boosterTime = (seq.head + result) - constructionTime | |
if(canBooster(boosterTime, seq.tail)){ | |
//println(seq.head) | |
println(s"boosterTime $boosterTime result $result") | |
solve2(nOfBoosters - 1 , seq.tail, result + seq.head - (boosterTime / 2)) | |
}else { | |
solve2(nOfBoosters, seq.tail, result + seq.head) | |
} | |
// 부스터를 안쓴다. | |
}else { | |
solve2(nOfBoosters, seq.tail, result + seq.head) | |
} | |
//seq.tail // 나머 | |
} | |
def solve = { | |
val seq = getSeq.map(_ * 2); | |
println(solve2(nOfBoosters,seq)) | |
//부스터 개수랑 나머지 개수랑 | |
} | |
def getSeq = { | |
val fac = (nOfStars / distances.length) + 1 | |
val res = (1 to fac).toList.map(_ => distances).flatten | |
res.take(nOfStars) | |
} | |
} | |
def main(args: Array[String]) { | |
val nOfCases: Int = in.next.toInt | |
val testCases = for { | |
i <- 1 to nOfCases | |
val line = in.next.split(" ").map(_.toInt).toList | |
} yield { | |
line match { | |
case a :: b :: c :: _ :: e => TestCase(a, b, c, e) | |
} | |
} | |
println(testCases.map(_.solve)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment