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?

AtCoder Beginner Contest 037

Last updated at Posted at 2024-09-12

C - 総和

解説

  • 累積和の配列を作る
    • 0番目の要素は0になるようにする
  • (k + i までの累積和) - (i までの累積和)

実装例(C++)

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;

    vector<long> a(n);

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<long> sum(n+1);

    for (int i = 0; i < n; i++) {
        sum[i + 1] = sum[i] + a[i];
    }

    long total = 0;
    for (int i = 0; i < n - k + 1; i++) {
        total += sum[k + i] - sum[i];
    }

    cout << total <<endl;
}
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?