Skip to content

Instantly share code, notes, and snippets.

@uchidama
Created July 15, 2021 15:11
Show Gist options
  • Save uchidama/d49424e0d37c984247f497f38b387d51 to your computer and use it in GitHub Desktop.
Save uchidama/d49424e0d37c984247f497f38b387d51 to your computer and use it in GitHub Desktop.
AtCoder Beginner Contest 032 [ C - 列 ] https://atcoder.jp/contests/abc032/tasks/abc032_c
'''
[問題]
https://atcoder.jp/contests/abc032/tasks/abc032_c
 しゃくとり法の練習問題
[解法]
Pythonでしゃくとり法(尺取り法)を実装してみる-ABC032
https://nashidos.hatenablog.com/entry/2020/02/02/165319
 ここ参考
[結果]
PyPy3(7.3.0) AC 85ms
Python(3.8.2) AC 122ms
'''
import sys
sys.setrecursionlimit(10 ** 6) # 再帰上限の引き上げ
input = sys.stdin.readline
INF = 2 ** 63 - 1
N, K = map(int, input().split())
S = [int(input()) for _ in range(N)]
if 0 in S:
# Sに0が含まれている時は、全ての積は0なので0 <= Kなら積は常にK以下。よってNが答え
print(N)
exit()
r, ans, tmp = 0, 0, 1
for l in range(N):
# lを固定してrを、ずらしていく。ループ条件が複数の場合、PythonだとWhileのこの書き方がベストかな
while (r < N and tmp*S[r] <= K):
tmp *= S[r]
r += 1
ans = max(ans, r-l)
if l == r:
r += 1
else:
# lが1増えるので、今のlで割っておく
tmp //= S[l]
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment