結果

問題 No.429 CupShuffle
ユーザー ichibanshiboriichibanshibori
提出日時 2016-10-05 15:19:04
言語 OCaml
(5.1.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 1,761 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 20,224 KB
実行使用メモリ 17,020 KB
最終ジャッジ日時 2024-04-17 08:34:19
合計ジャッジ時間 1,529 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 16 ms
6,940 KB
testcase_11 AC 138 ms
17,020 KB
testcase_12 AC 134 ms
17,020 KB
testcase_13 AC 139 ms
17,016 KB
testcase_14 AC 136 ms
17,016 KB
testcase_15 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

let ( @! ) lst n = List.nth lst n

let read_AB_arr k x =
  let abArr = Array.make k (0, 0) in
  let rec read_AB_arr' i =
    if i >= k then abArr
    else (
      let line = read_line () in
      let a, b =
      	if i = (x - 1) then (-1, -1)
        else
          Str.split (Str.regexp_string " ") line |>
          List.map int_of_string |>
          (fun lst -> (lst @! 0, lst @! 1))
      in
      abArr.(i) <- (a, b);
      read_AB_arr' (i + 1))
  in
  read_AB_arr' 0

let do_swap_cup (cupArr: int array) abArr startIdx step =
  let swap_cup a b =
    let tmp = cupArr.(a - 1) in
    cupArr.(a - 1) <- cupArr.(b - 1);
    cupArr.(b - 1) <- tmp
  in
  let rec do_swap_cup' idx =
  	let a, b = abArr.(idx) in
    if a <> -1 then (
      swap_cup a b;
      do_swap_cup' (idx + step))
  in
  do_swap_cup' startIdx

let diff_cups n arr1 arr2 =
  let rec diff_cups' idx cnt result =
    if idx >= n || cnt = 2 then result
    else (
      let nextCnt, nextResult =
        if arr1.(idx) <> arr2.(idx) then
          (cnt + 1), (idx + 1) :: result
        else cnt, result
      in
      diff_cups' (idx + 1) nextCnt nextResult)
  in
  diff_cups' 0 0 [] |>
  List.rev

let () =
  let n, k, x = read_line () |>
                Str.split (Str.regexp_string " ") |>
                List.map int_of_string |>
                (fun lst -> (lst @! 0, lst @! 1, lst @! 2))
  in
  let abArr = read_AB_arr k x
  and cArr = read_line () |>
             Str.split (Str.regexp_string " ") |>
             List.map int_of_string |>
             Array.of_list
  and cupArr = Array.init n (fun i -> i + 1)
  in
  do_swap_cup cupArr abArr 0 1;
  do_swap_cup cArr abArr (k - 1) (-1);
  diff_cups n cupArr cArr |>
  List.map string_of_int |>
  String.concat " " |>
  print_endline
0