結果

問題 No.3392 Count 23578 Sequence
コンテスト
ユーザー mkawa2
提出日時 2025-11-28 23:19:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 462 ms / 2,000 ms
コード長 1,919 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 81,988 KB
実行使用メモリ 270,936 KB
最終ジャッジ日時 2025-11-28 23:32:58
合計ジャッジ時間 23,666 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)

# md = 10**9+7
md = 998244353

# 偶数長、奇数長含めて回文「直径」(つまり全長)を返す
# s[i]の前の隙間を中心とした回文長がres[i*2](偶数長)
# s[i]を中心とした回文長がres[i*2+1](奇数長)
#  a b a a b a
# 0103016103010
# dummyがsに入っていないか注意
def manacher(target):
    def same(i, j, w):
        if s[i] == None: return True
        return s[i]+s[j] == w

    s = [None]*(2*len(target)+1)
    for i, c in enumerate(target): s[i*2+1] = c
    # print(s)
    i = j = 1
    res = [0]*len(s)
    while i < len(s)-1:
        w = s[i-1]+s[i+1] if s[i] == None else s[i]*2
        while i-j >= 0 and i+j < len(s) and same(i-j, i+j, w): j += 1
        res[i] = j-1
        k = 1
        while i-k >= 0 and k+res[i-k]+1 < j:
            res[i+k] = res[i-k]
            k += 1
        i += k
        j -= k
    return res
    # 文字列を返したいときは最終行を消して、以下に書き換える
    # mx = max(res)
    # i = res.index(mx)
    # if mx & 1: l = i-mx//2*2
    # else: l = i-mx+1
    # return s[l:l+mx*2:2]

n=II()
aa=LI()
dd=manacher(aa)
ans=0
for d in dd:
    ans+=(d+1)//2
print(ans)
0