Skip to content

Instantly share code, notes, and snippets.

@akihiro4chawon
Created April 1, 2011 13:43
Show Gist options
  • Save akihiro4chawon/898160 to your computer and use it in GitHub Desktop.
Save akihiro4chawon/898160 to your computer and use it in GitHub Desktop.
競技プログラミングモードのscalaを書いてみたw
// お題:時間帯重複チェック(応用編) 別の答案(この答案は、入力チェックはしておりません)
// <http://d.hatena.ne.jp/fumokmm/20110329/1301403400>
//
// (こちらの解答を参考にした後に作成しました → http://d.hatena.ne.jp/route150/20110401/1301661345
object Test extends Application {
type TSpan = (Int, Int, Int, Int)
def solve(ts: TSpan*) = {
val toAns: Seq[Int] => TSpan = { case Seq(a, b) => (a / 60, a % 60, b / 60, b % 60) }
val dup = (ts flatMap {case (h1, m1, h2, m2) => h1 * 60 + m1 until h2 * 60 + m2} groupBy identity collect { case(x, l) if l.size > 1 => x}).toSeq.sorted
((-2 +: dup :+ -2) sliding 2 collect {case Seq(a, b) if b - a != 1 => Seq(a + 1, b)}).toSeq.flatten filter {_ >= 0} grouped 2 map toAns
}
for {arg <-Seq(
// 例1(出力:(12, 0, 12, 15))
Seq((12, 0, 13, 0), (10, 0, 12, 15)),
// 例2(出力:(9, 0, 10, 30), (16, 0, 17, 0))
Seq((16, 0, 23, 0), (9, 0, 17, 0), (5, 0, 10, 30)),
// 例3(出力:(13, 0, 14, 0), (15, 0, 16, 0), (17, 0, 18, 0), (19, 0, 20, 0), (21, 0, 22, 0))
Seq((12, 0, 23, 0), (13, 0, 14, 0), (15, 0, 16, 0), (17, 0, 18, 0), (19, 0, 20, 0), (21, 0, 22, 0)),
// 例4(出力:(10, 30, 11, 30))
Seq((10, 0, 12, 0), (11, 0, 11, 30), (10, 30, 11, 15)),
// 例5(なし)
Seq((9, 0, 17, 0), (19, 0, 21, 0)),
// コーナケース1(なし)
Seq((1, 0, 2, 0), (2, 0, 3, 0)),
// コーナケース2((1, 59, 2, 0))
Seq((1, 0, 2, 0), (1, 59, 3, 0)),
// コーナケース3((0, 0, 0, 1), (23, 59, 24, 0))
Seq((0, 0, 24, 0), (0, 0, 0, 1), (23, 59, 24, 0))
)} println(solve(arg :_*) mkString ",")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment