Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

ABC024をHaskellで

0
Posted at

A - 動物園

問題 ABC024A

シグネチャを決める。横着する。

abc024a :: [Int] -- A,B,C,K
        -> [Int] -- S,T
        -> Int   -- 答え

問題文どおりに計算する。

結果

abc024a [a,b,c,k] [s,t]
  | k <= st   = base - c * st
  | otherwise = base
  where
    st = s + t
    base = a * s + b * t

B - 自動ドア

問題 ABC024B

シグネチャを決める。

abc024b :: Int   -- N
        -> Int   -- T
        -> [Int] -- Ai
        -> Int   -- 答え

それぞれの人について、次の人が到着するまでの時間とTのどちらか短い方だけ開いている。
最後の人については必ずT開く。

結果

abc024b :: Int -> Int -> [Int] -> Int
abc024b n t as = t + sum (between f as)
  where
    f a0 a1 = min t (a1 - a0)
    between f xs = zipWith f xs $ tail xs

ところで、$T \leq 10^6$ 秒は 278 時間なので、随分一日が長い惑星である。

公式解法回りくどくないか。

C - 民族大移動

問題 ABC024C

シグネチャを決める。横着する。

abc024c :: [Int]   -- N,D,K
        -> [[Int]] -- Li,Ri
        -> [[Int]] -- Si,Ti
        -> [Int]   -- 答え

ひとつの街に一民族しかいられないとか、グラフの辺をたどるとか、そういう要素はないようだ。

各民族について個別に調べる。
移動できるときにしたりしなかったりしたとき、d 日目に到達できる街の区間を管理する。

  • 初期値は $[S_i, S_i]$
  • d-1 日めの結果が $[A, B]$ のとき、これと $[L_d, R_d]$ が交差するなら次は和集合、しないなら元のまま
  • $T_i$ を含むようになった日付が答え

結果

abc024c :: Int -> Int -> Int -> [[Int]] -> [[Int]] -> [Int]
abc024c n d k lrs = map f
  where
    f (s:t:_) = go s s $ zip [1..] lrs
      where
        go a b ((d, l:r:_):ilrs)
          | is, goal  = d
          | is        = go (min a l) (max b r) ilrs
          | otherwise = go a b ilrs
          where
            is   = a <= r && l <= b -- 交差がある intersect
            goal = l <= t && t <= r -- 目的地を含んでいる

D - 動的計画法

問題 ABC024D

シグネチャを決める。横着する。

abc024d :: [Int]   -- A,B,C
        -> [Int]   -- 答え
  • マス $(r,c)$ にある数 $A = _{r+c} C_r$
  • マス $(r,c+1)$ にある数 $B = _{r+c+1} C_r$
  • マス $(r+1,c)$ にある数 $C = _{r+c+1} C_{r+1}$

定義から $\displaystyle A = \frac{(r+c)!}{r! \cdot c!}, B = \frac{r+c+1}{c+1} A, C = \frac{r+c+1}{r+1} A$ となる。
さらに$d = r + c + 1$ と定義してこねくり回すと $A/B = (c+1)/d, A/C = (r+1)/d, A/B + A/C = (r+c+2)/d = 1 + 1/d$
1引いて $1/d = A/B + A/C - 1 = (AB - BC + CA) / BC$
$D = AB - BC + CA$ と定義して式変形すると $c = B(C-A)/D, r = C(B-A)/D$ が導ける。

結果

abc024d :: [Int] -> [Int]
abc024d [a,b,c] = [x, y]
  where
    d = modRecip $ summ [mul a b, - mul b c, mul c a]
    x = prodd [c, reg $ b - a, d]
    y = prodd [b, reg $ c - a, d]

一意に定まらない場合云々はフェイクだったか。

公式解説

式の形が違うけど、結果は同じなのか。逆元がひとつだけでいいこちらの式の方がかっこよくない?

省略された部分は $\frac{c}{c+1} = X$ として $\frac{1}{X} = \frac{c+1}{c} = 1 + \frac{1}{c}$, $c = \frac{X}{1-X}$

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?