0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

yukicoder contest 252 参戦記

Last updated at Posted at 2020-06-12

yukicoder contest 252 参戦記

A 1076 寿司打

モンテカルロ法は駄目だったので、真面目に数学しました. 求める期待値を n とすると n = p * (1 + n) + (1 - p) * 0 となるので n = p / (1 - p) となります.

p = float(input())

print(p / (1 - p))

B 1077 Noelちゃんと星々4

「広義単調増加 競プロ」で検索すると、POJ-3666 : Making the Grade のコードがでてくるので、広義単調減少列側のコードをコメントアウトして投稿すると解けました(酷).

追記: ちゃんと解きました. 移動後の Yi-1 が x の時にそれまでの移動させた距離の総和を dp[i - 1][x] とすると、dp[i][x] は min(dp[i - 1][0], dp[i - 1][1], ..., dp[i - 1][x]) + abs(Yi - x) となる.

N = int(input())
Y = list(map(int, input().split()))

dp = [[0] * (10 ** 4 + 1) for _ in range(N)]

for i in range(N):
    t = float('inf')
    for j in range(10 ** 4 + 1):
        t = min(t, dp[i - 1][j])
        dp[i][j] = t + abs(Y[i] - j)

print(min(dp[N - 1]))

これだと Python では TLE になり PyPy でしか通らない. ところで2次元で書いているが、実際はオーバーラップするところがないので、1次元でも動く. また、Yi の移動先の座標は Y1, ..., YN のどれかの座標のみとして問題ない. 以上を考慮して書くと、Python でも通るコードになる.

N = int(input())
Y = list(map(int, input().split()))

a = sorted(set(Y))
dp = [0] * len(a)

for i in range(N):
    t = float('inf')
    for j in range(len(a)):
        t = min(t, dp[j])
        dp[j] = t + abs(Y[i] - a[j])

print(min(dp))
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?