結果

問題 No.1767 BLUE to RED
ユーザー yudedakoyudedako
提出日時 2022-02-02 13:57:54
言語 Scala(Beta)
(3.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,396 bytes
コンパイル時間 10,194 ms
コンパイル使用メモリ 262,984 KB
実行使用メモリ 124,724 KB
最終ジャッジ日時 2023-09-02 02:45:37
合計ジャッジ時間 56,859 ms
ジャッジサーバーID
(参考情報)
judge16 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 967 ms
62,996 KB
testcase_01 AC 962 ms
62,640 KB
testcase_02 AC 963 ms
62,524 KB
testcase_03 AC 976 ms
62,588 KB
testcase_04 AC 991 ms
62,660 KB
testcase_05 AC 1,005 ms
62,444 KB
testcase_06 AC 962 ms
62,536 KB
testcase_07 AC 1,000 ms
62,520 KB
testcase_08 AC 1,015 ms
62,768 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*

class UnionFind(val size: Int):
  private val vec = Array.fill(size){-1}
  def find(a: Int): Int =
    if vec(a) < 0 then
      a
    else
      vec(a) = find(vec(a))
      vec(a)
  def same(a: Int, b: Int): Boolean = find(a) == find(b)
  def unite(a: Int, b: Int): Unit =
    val i = find(a)
    val j = find(b)
    if i != j then
      if vec(i) < vec(j) then
        vec(i) += vec(j)
        vec(j) = i
      else
        vec(j) += vec(i)
        vec(i) = j
@main def main =
  val Array(n, m) = readLine().split(' ').map(_.toInt)
  val a = readLine().split(' ').map(_.toInt)
  val b = readLine().split(' ').map(_.toInt)
  val allCell = Array.fill(n + m){0}
  val isRed = Array.fill(n + m){false}
  {
    var ai = 0
    var bi = 0
    for i <- 0 until n + m do
      if bi == m || (ai != n && a(ai) < b(bi)) then
        allCell(i) = a(ai)
        isRed(i) = true
        ai += 1
      else
        allCell(i) = b(bi)
        bi += 1
  }
  val uft = UnionFind(n + m + 1)
  for i <- 0 until n + m if isRed(i) do
    uft.unite(i, n + m)
  var result = 0
  for i <- (1 until n + m).sortBy(i => allCell(i) - allCell(i - 1)) do
    if !uft.same(i, i - 1) then
      result += allCell(i) - allCell(i - 1)
      uft.unite(i, i - 1)
  println(result)
0