Skip to content

Instantly share code, notes, and snippets.

@KimDaesap
Last active December 11, 2015 13:18
Show Gist options
  • Save KimDaesap/700d530f59ef68ae83cc to your computer and use it in GitHub Desktop.
Save KimDaesap/700d530f59ef68ae83cc to your computer and use it in GitHub Desktop.
import java.io.PrintWriter
import scala.StringBuilder
object Codejam {
// Config.
val inputName = "D-small-practice"
val inputFilePath = inputName + ".in"
val outputFilePath = inputName + ".out"
val isConsole = false
// Main procedure.
def main(args: Array[String]): Unit = {
val inputLines = scala.io.Source.fromFile(inputFilePath).getLines()
val writer = if (isConsole) new PrintWriter(Console.out)
else new PrintWriter(outputFilePath)
try {
// 입력 케이스 수 T 만큼 입력 처리.
(1 to inputLines.next().toInt).foreach(
(num) => writer.println(s"Case #$num: ${process(inputLines)}"))
}
finally {
writer.flush()
writer.close()
}
}
/*
문제풀이.
*/
// Process input value.
def process(inputLines: Iterator[String]): String = {
// 파싱
val Array(n,k,c,x) = inputLines.next().split(" ").map(_.toInt)
val a = inputLines.next().split(" ").map(_.toInt)
val b = inputLines.next().split(" ").map(_.toInt)
// N 행렬 생성
val matrix = Array.ofDim[Long](n, n)
for (i <- 1 to n) {
for (j <- 1 to n) {
matrix(i-1)(j-1) = (a(i-1) * i + b(j-1) * j + c) % x
}
}
solve(matrix, n, k).toString
}
def solve(matrix: Array[Array[Long]], n: Int, k: Int): Long = {
var maxs: List[Long] = List()
for (i <- 0 to n - k) {
for (j <- 0 to n - k) {
var maxList: List[Long] = List()
for (m <- 0 until k) {
maxList ++= matrix((i + m).toInt).drop(j).take(k)
}
maxs ::= maxList.max
}
}
maxs.sum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment