Created
May 21, 2014 01:32
-
-
Save pocarist/16100545fb92abb848ff to your computer and use it in GitHub Desktop.
結城 浩さんからのアルゴリズムの問題(チケットゴブル社の旅行プランを作れ!) https://codeiq.jp/ace/yuki_hiroshi/q863
This file contains hidden or 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
(* POINT: 選べるチケットの中で帰国日が最も早いものを選ぶことを繰り返す。Greedy。 | |
アリ本P.43と同じ問題。 *) | |
(* OCaml *) | |
let try_fx f x = | |
try Some (f x) with | |
| _ -> None | |
let () = | |
(* 日付の比較は単に(month, day)のペアとする *) | |
(* 入力の各行を((to, from), country)のペアに並び替える *) | |
let read_line () = Scanf.scanf "%s %d/%d-%d/%d " | |
(fun a b c d e -> ((d,e), (b,c)), a) | |
in | |
(* 帰国日が早い順にソート *) | |
let read_problem () = | |
let rec loop acc = | |
try_fx read_line () |> function | |
| None -> List.sort compare acc | |
| Some x -> loop (x::acc) | |
in | |
loop [] | |
in | |
(* 重複しないようにする *) | |
let rec loop t acc = function | |
| [] -> List.sort compare acc | |
| ((_to, _from), country) :: xs -> | |
if t < _from then | |
loop _to (country :: acc) xs | |
else | |
loop t acc xs | |
in | |
let period_sorted = read_problem () in | |
let ans = loop (0,0) [] period_sorted in | |
ans | |
|> String.concat " " | |
|> Printf.printf "%d %s\n" (List.length ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment