Submission #3728792


Source Code Expand

module S =
  Set.Make (struct
      type t = int
      let compare = compare
    end)

let id x = x

let init n f =
  let rec loop i l = if i < n then loop (i + 1) (f i :: l) else l in
  List.rev (loop 0 [])

let rec is_sorted = function
    [] -> true
  | [_] -> true
  | x :: y :: l -> if compare x y <= 0 then is_sorted (y :: l) else false

let n = Scanf.scanf " %d" id
let m = Scanf.scanf " %d" id
let q = Scanf.scanf " %d" id
let a =
  init q (fun _ -> Scanf.scanf " %d" id)
  |> List.rev

let () =
  let cnt = Array.make (m + 1) 0 in
  let nxt = Array.make (m + 1) None in
  let root = ref (-1) in
  let f (cur, ts) a =
    if cnt.(a) > 0 then
      let nxt = match nxt.(a) with
          Some nxt -> nxt
        | None -> raise (Failure "") in
      let () = cnt.(a) <- cnt.(a) - 1;
               cnt.(nxt) <- cnt.(nxt) + 1 in
      (cur, ts)
    else
      match nxt.(a) with
        Some _ -> (cur, ts) (* do nothing *)
      | None -> match cur with
                  None -> let () = root := a;
                                   nxt.(a) <- Some 0;
                                   cnt.(a) <- cnt.(0);
                                   cnt.(0) <- 0 in
                          (Some a, S.remove a ts)
                | Some cur -> let () = nxt.(cur) <- Some a;
                                       nxt.(a) <- Some 0;
                                       cnt.(a) <- cnt.(0);
                                       cnt.(0) <- 0 in
                              (Some a, S.remove a ts) in
  let () = cnt.(0) <- n - 1 in
  let (_, ts) = List.fold_left f (None, S.of_list (init m (fun i -> i + 1))) a in
  let hs =
    let rec loop i f l =
      match nxt.(i) with
        None -> l
      | Some nxt -> if cnt.(i) > 0 || f then
                      loop nxt true (i :: l)
                    else
                      loop nxt f l in
    List.rev (loop !root false []) in
  let mx = List.fold_left max min_int hs in
  let mn = try S.min_elt ts with Not_found -> max_int in
  if is_sorted hs && mx <= mn then
    print_endline "Yes"
  else
    print_endline "No"

Submission Info

Submission Time
Task E - LRU Puzzle
User iwashisnake
Language OCaml (4.02.3)
Score 1200
Code Size 2144 Byte
Status AC
Exec Time 195 ms
Memory 20256 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1200 / 1200
Status
AC × 4
AC × 76
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt, 1_50.txt, 1_51.txt, 1_52.txt, 1_53.txt, 1_54.txt, 1_55.txt, 1_56.txt, 1_57.txt, 1_58.txt, 1_59.txt, 1_60.txt, 1_61.txt, 1_62.txt, 1_63.txt, 1_64.txt, 1_65.txt, 1_66.txt, 1_67.txt, 1_68.txt, 1_69.txt, 1_70.txt, 1_71.txt
Case Name Status Exec Time Memory
0_00.txt AC 1 ms 384 KB
0_01.txt AC 1 ms 384 KB
0_02.txt AC 1 ms 384 KB
0_03.txt AC 1 ms 384 KB
1_00.txt AC 1 ms 384 KB
1_01.txt AC 1 ms 384 KB
1_02.txt AC 110 ms 17952 KB
1_03.txt AC 110 ms 17952 KB
1_04.txt AC 110 ms 17952 KB
1_05.txt AC 111 ms 17952 KB
1_06.txt AC 152 ms 17952 KB
1_07.txt AC 153 ms 17952 KB
1_08.txt AC 152 ms 17952 KB
1_09.txt AC 152 ms 17952 KB
1_10.txt AC 82 ms 12928 KB
1_11.txt AC 82 ms 12928 KB
1_12.txt AC 82 ms 12928 KB
1_13.txt AC 84 ms 12928 KB
1_14.txt AC 82 ms 12928 KB
1_15.txt AC 82 ms 12928 KB
1_16.txt AC 82 ms 12928 KB
1_17.txt AC 84 ms 12928 KB
1_18.txt AC 130 ms 17440 KB
1_19.txt AC 117 ms 12960 KB
1_20.txt AC 141 ms 17184 KB
1_21.txt AC 195 ms 20128 KB
1_22.txt AC 154 ms 20128 KB
1_23.txt AC 132 ms 17184 KB
1_24.txt AC 137 ms 18080 KB
1_25.txt AC 125 ms 14464 KB
1_26.txt AC 35 ms 9472 KB
1_27.txt AC 34 ms 9472 KB
1_28.txt AC 38 ms 9600 KB
1_29.txt AC 39 ms 9728 KB
1_30.txt AC 82 ms 14880 KB
1_31.txt AC 84 ms 14592 KB
1_32.txt AC 70 ms 12928 KB
1_33.txt AC 90 ms 15008 KB
1_34.txt AC 67 ms 11648 KB
1_35.txt AC 85 ms 12928 KB
1_36.txt AC 61 ms 11648 KB
1_37.txt AC 90 ms 12928 KB
1_38.txt AC 123 ms 15008 KB
1_39.txt AC 124 ms 15136 KB
1_40.txt AC 134 ms 20000 KB
1_41.txt AC 186 ms 20256 KB
1_42.txt AC 35 ms 9472 KB
1_43.txt AC 35 ms 9472 KB
1_44.txt AC 35 ms 9472 KB
1_45.txt AC 35 ms 9472 KB
1_46.txt AC 44 ms 11264 KB
1_47.txt AC 59 ms 12928 KB
1_48.txt AC 59 ms 12928 KB
1_49.txt AC 95 ms 15776 KB
1_50.txt AC 38 ms 9728 KB
1_51.txt AC 35 ms 9472 KB
1_52.txt AC 41 ms 10240 KB
1_53.txt AC 39 ms 9728 KB
1_54.txt AC 91 ms 15008 KB
1_55.txt AC 58 ms 12928 KB
1_56.txt AC 106 ms 18720 KB
1_57.txt AC 98 ms 16544 KB
1_58.txt AC 33 ms 9472 KB
1_59.txt AC 34 ms 9472 KB
1_60.txt AC 33 ms 9472 KB
1_61.txt AC 34 ms 9472 KB
1_62.txt AC 56 ms 12928 KB
1_63.txt AC 94 ms 18080 KB
1_64.txt AC 44 ms 11648 KB
1_65.txt AC 94 ms 15008 KB
1_66.txt AC 32 ms 9472 KB
1_67.txt AC 32 ms 9472 KB
1_68.txt AC 32 ms 9472 KB
1_69.txt AC 33 ms 9472 KB
1_70.txt AC 90 ms 17056 KB
1_71.txt AC 87 ms 14880 KB