Submission #345942


Source Code Expand

let pf = Printf.printf ;;
let epf = Printf.eprintf ;;
let sf = Scanf.scanf ;;

let (|>) x f = f x ;;
let (@@) f x = f x ;;

module Array = struct
  include ArrayLabels
  let foldi ~f ~init arr =
    let acc = ref init in
    for i = 0 to Array.length arr - 1 do
      acc := f i !acc arr.(i)
    done;
    !acc
  ;;

  let findi arr ~f =
    let len = Array.length arr in
    let rec iter i =
      if i = len then None
      else if f arr.(i) then Some i
      else iter (i + 1) in
    iter 0
  ;;

  let findi_exn arr ~f =
    let len = Array.length arr in
    let rec iter i =
      if i = len then None
      else if f arr.(i) then Some i
      else iter (i + 1) in
    iter 0
  ;;

  (* i ∈ [0..res) -> not (f i) /\ i ∈ [res, len) -> f i *)
  let lower_bound arr ~f =
    let l, h = 0, Array.length arr in
    (* res ∈ (l, h] *)
    let rec iter l h =
      if l = h - 1 then h
      else 
        let m = (l + h) lsr 1 in
        if f m then iter l m
        else iter m h in
    iter l h
  ;;

  (* i ∈ [0..res] -> f i /\ i ∈ (res, len) -> not (f i) *)
  let upper_bound arr ~f =
    let l, h = 0, Array.length arr in
    (* res ∈ [l, h) *)
    let rec iter l h =
      if l = h - 1 then l
      else 
        let m = (l + h) lsr 1 in
        if f m then iter m h
        else iter l m in
    iter l h
  ;;
end
  
module String = StringLabels ;;
module List  = struct
  include ListLabels ;;
  let rec repeat n a = if n = 0 then [] else a :: repeat (n - 1) a ;;
  let rec drop n a =
    if n = 0 then a
    else match a with
    | [] -> failwith "cannot take"
    | x :: xs -> drop (n - 1) xs ;;

  let init ~f n =
    let res = ref [] in
    for i = 0 to n - 1 do
      res := f i :: !res
    done;
    List.rev !res
  ;;
end ;;
module H = Hashtbl ;;

let c = ref ' ' ;;
let is_num c = '0' <= c && c <= '9' ;;
let is_space c = c = ' ' || c = '\n' || c = '\t' ;;
let to_num c = Char.code c - Char.code '0' ;;
let read_char () = input_char stdin ;;

let next_int () =
  while is_space !c do
    c := read_char () ;
  done;
  let ok = ref false in
  let acc = ref 0 in
  while is_num !c do
    ok := true;
    acc := !acc * 10 + to_num !c;
    c := read_char ();
  done;
  if !ok then !acc
  else raise (Failure "next_int")
;; 

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

let solve n =
  let ds = Array.make n 0 in
  for i = 1 to n - 1 do
    pf "? %d %d\n" 1 (i + 1); flush stdout;
    sf "%d\n"  (fun d -> ds.(i) <- d)
  done;
  let (midx, _) = Array.foldi ds ~init:(-1, 0) ~f:(fun i (mi, mx) x -> 
    if x >= mx then (i, x) else (mi, mx)) in
  let mx = ref (-1) in
  for i = 0 to n - 1 do
    pf "? %d %d\n" (midx + 1) (i + 1); flush stdout;
    sf "%d\n" (fun x -> mx := max !mx x);
  done;
  pf "! %d\n" !mx
;;

let () =
  sf "%d\n" (fun n -> solve n)

Submission Info

Submission Time
Task D - 高橋くんと木の直径
User iab
Language OCaml (3.12.1)
Score 100
Code Size 2933 Byte
Status AC
Exec Time 63 ms
Memory 2704 KiB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 20 / 20 80 / 80
Status
AC × 1
AC × 22
AC × 42
Set Name Test Cases
Sample subtask0_0.txt
Subtask1 subtask1_0.txt, subtask1_1.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_2.txt, subtask1_20.txt, subtask1_3.txt, subtask1_4.txt, subtask1_5.txt, subtask1_6.txt, subtask1_7.txt, subtask1_8.txt, subtask1_9.txt, subtask0_0.txt
All subtask0_0.txt, subtask1_0.txt, subtask1_1.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_2.txt, subtask1_20.txt, subtask1_3.txt, subtask1_4.txt, subtask1_5.txt, subtask1_6.txt, subtask1_7.txt, subtask1_8.txt, subtask1_9.txt, subtask2_0.txt, subtask2_1.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt, subtask2_13.txt, subtask2_14.txt, subtask2_15.txt, subtask2_16.txt, subtask2_17.txt, subtask2_18.txt, subtask2_19.txt, subtask2_2.txt, subtask2_3.txt, subtask2_4.txt, subtask2_5.txt, subtask2_6.txt, subtask2_7.txt, subtask2_8.txt, subtask2_9.txt
Case Name Status Exec Time Memory
subtask0_0.txt AC 43 ms 2300 KiB
subtask1_0.txt AC 63 ms 2632 KiB
subtask1_1.txt AC 55 ms 2624 KiB
subtask1_10.txt AC 56 ms 2684 KiB
subtask1_11.txt AC 56 ms 2628 KiB
subtask1_12.txt AC 45 ms 2632 KiB
subtask1_13.txt AC 60 ms 2616 KiB
subtask1_14.txt AC 45 ms 2668 KiB
subtask1_15.txt AC 51 ms 2620 KiB
subtask1_16.txt AC 47 ms 2620 KiB
subtask1_17.txt AC 53 ms 2624 KiB
subtask1_18.txt AC 54 ms 2616 KiB
subtask1_19.txt AC 58 ms 2624 KiB
subtask1_2.txt AC 63 ms 2608 KiB
subtask1_20.txt AC 57 ms 2320 KiB
subtask1_3.txt AC 54 ms 2620 KiB
subtask1_4.txt AC 59 ms 2624 KiB
subtask1_5.txt AC 44 ms 2620 KiB
subtask1_6.txt AC 43 ms 2676 KiB
subtask1_7.txt AC 44 ms 2672 KiB
subtask1_8.txt AC 59 ms 2696 KiB
subtask1_9.txt AC 55 ms 2704 KiB
subtask2_0.txt AC 53 ms 2684 KiB
subtask2_1.txt AC 52 ms 2692 KiB
subtask2_10.txt AC 54 ms 2620 KiB
subtask2_11.txt AC 46 ms 2672 KiB
subtask2_12.txt AC 45 ms 2676 KiB
subtask2_13.txt AC 44 ms 2672 KiB
subtask2_14.txt AC 44 ms 2672 KiB
subtask2_15.txt AC 43 ms 2592 KiB
subtask2_16.txt AC 47 ms 2668 KiB
subtask2_17.txt AC 46 ms 2700 KiB
subtask2_18.txt AC 43 ms 2676 KiB
subtask2_19.txt AC 44 ms 2664 KiB
subtask2_2.txt AC 44 ms 2664 KiB
subtask2_3.txt AC 45 ms 2672 KiB
subtask2_4.txt AC 45 ms 2688 KiB
subtask2_5.txt AC 45 ms 2680 KiB
subtask2_6.txt AC 45 ms 2680 KiB
subtask2_7.txt AC 46 ms 2664 KiB
subtask2_8.txt AC 46 ms 2660 KiB
subtask2_9.txt AC 44 ms 2624 KiB