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?

ABC379

Posted at

ABC379

久しぶりにAtCoderをやりました. アカウントです.
結果はA,Bはいけましたが、Cで時間オーバーでした. まずA,Bです.

import sys

input = sys.stdin.readline
N = input()
mojis = [char for char in N]
a = mojis[1] + mojis[2] + mojis[0]
b = mojis[2] + mojis[0] + mojis[1]
a = int(a)
b = int(b)
print(a, b)
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
s = list(input())
count = 0
mask = []
for i in range(len(s)):
    if s[i] == "X":
        mask.append(count)
        count = 0
    elif i == len(s) - 1:
        mask.append(count)
    else:
        count += 1
ans = sum(i / k for i in mask)
print(ans)

C振り返り

失敗コード

import sys

input = sys.stdin.readline
n, m = map(int, input().split())
s = [input().split() for _ in range(2)]
ans = -1
dp = [0] * (n + 1)
for num, let in zip(s[0], s[1]):
    num = int(num)
    let = int(let)
    dp[num] = let
ans = -1
if (sum(dp) != n) or (dp[1] == 0):
    print(-1)
else:
    for num in range(m):
        i = int(s[0][num])
        j = 0
        while dp[i] >= 2:
            j += 1
            ans += j
            dp[i] -= 1
            if i + j < n + 1:
                dp[i + j] += 1
        if dp.count(0) > 1:
            if dp[int(s[0][num + 1]) - 1] == 0:
                ans = -1
                break
        ans += 1
    print(ans)

このコードの失敗点は,以下の内容です.

  • Nの大きさを考慮していなかった.
  • Nが大きすぎて全探索は無理で上記のコードはどのみち無理だった
  • ソートアルゴリズムなどのアルゴリズムの種類を考えることなく解いたのが原因

公式の解説出会った通り解こうとしても, pythonにはpairsは無いからそこを工夫する必要があるが,分りませんでした.

0
0
2

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?