1
1

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.

競技プログラミングでTLEのときに高速化できるアルゴリズム

Last updated at Posted at 2020-04-07

暇があるときに更新する、競プロをやっているときに気づいたことの備忘録

Level1

累積和

配列の部分和を求め比較する必要がある際、一回づつ部分和を求めるのではなく、前項(i項まで)の和にi+1の項を加えてそこから部分和の範囲を考慮したほうが早い。例上記が正解、下記だとTLEになる。

ABC_D_Dice_in_Line.py
for i in range(k, len(p)):
    saveSum = saveSum + p[i] - p[i - k]
    if saveSum > M:
        M = saveSum

ABC_D_Dice_in_Line.py
for i in range(len(p) - k + 1):
    # 一回づつlistの和を求めているから計算量が多い
    savesum = sum(p[i:i + k])
    if savesum > M:
        M = savesum

itertools

あとで書く

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?