結果
問題 | No.2745 String Swap Battle |
ユーザー |
|
提出日時 | 2024-04-21 12:03:34 |
言語 | OCaml (5.2.1) |
結果 |
AC
|
実行時間 | 344 ms / 2,000 ms |
コード長 | 2,423 bytes |
コンパイル時間 | 887 ms |
コンパイル使用メモリ | 22,528 KB |
実行使用メモリ | 20,680 KB |
最終ジャッジ日時 | 2024-10-13 10:26:26 |
合計ジャッジ時間 | 4,748 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
コンパイルメッセージ
File "Main.ml", line 53, characters 8-9: 53 | let k = Array.fold_left (fun acc s -> min acc (fst (calc_cost s max_int))) max_int s in ^ Warning 26 [unused-var]: unused variable k.
ソースコード
Scanf.scanf "%d" (fun n ->let module S = Set.Make (struct type t = int let compare = compare end) inlet calc_cost s limit =let len = String.length s inlet pos = Array.make 26 S.empty inlet arena = Array.make len 0 inString.iteri (fun i c ->let a = int_of_char c - 97 inpos.(a) <- S.add i pos.(a);arena.(i) <- a) s;let rec loop i cur count =if i = len then count elseif count = limit then count elseif arena.(i) = cur thenlet () = pos.(cur) <- S.remove i pos.(cur) inloop (i + 1) cur count elseif S.is_empty pos.(cur) then loop i (cur + 1) count elselet mp = S.max_elt pos.(cur) inlet () =let q = arena.(i) inpos.(cur) <- S.remove mp pos.(cur);pos.(q) <- S.add mp (S.remove i pos.(q));arena.(i) <- cur;arena.(mp) <- qinloop (i + 1) cur (count + 1)inlet count = loop 0 0 0 inlet check arena =let rec loop i =if i = len - 1 then false elseif arena.(i) = arena.(i + 1) then true else loop (i + 1)inloop 0inlet count = if limit = max_int then (if count = 0 then (if check arena then 1 else 2) else count) else (if (limit - count) mod 2 = 0 then count else (if check arena then limit else (let c = arena.(len - 1) inarena.(len - 1) <- arena.(len - 2);arena.(len - 2) <- c;limit)))inlet ret = String.init len (fun i -> char_of_int (97 + arena.(i))) incount, retinlet s = Array.init n (fun _ -> Scanf.scanf " %s" (fun s -> s)) inlet k = Array.fold_left (fun acc s -> min acc (fst (calc_cost s max_int))) max_int s inlet k = 1 inlet s2 = Array.map (fun s -> snd (calc_cost s k)) s inlet s3 = Array.copy s2 inArray.sort compare s3;let winner = Array.fold_left (fun acc v -> if v = s3.(0) then acc + 1 else acc) 0 s2 inArray.iter (fun v -> Printf.printf "%d\n" @@ if v = s3.(0) then n + 1 - winner else 0) s2;)