본문 바로가기

Algorithm_BOJ(백준)/누적합

[백준 14929 c++ V] 귀찮아 (SIB)

728x90
반응형

- 풀이 원본: 

https://github.com/xhaktmchl/Algorithm_study/blob/main/BOJ/%EB%88%84%EC%A0%81%ED%95%A9/%5B%EB%B0%B1%EC%A4%80%2014929%20c%2B%2B%20V%5D%20%EA%B7%80%EC%B0%AE%EC%95%84%20(SIB).cpp 

 

GitHub - xhaktmchl/Algorithm_study: 알고리즘 이론 및 문제풀이

알고리즘 이론 및 문제풀이. Contribute to xhaktmchl/Algorithm_study development by creating an account on GitHub.

github.com

#include <iostream>
#include <algorithm>
using namespace std;
/*
[백준 14929 c++ V] 귀찮아 (SIB)
문제:
접근1: 완탐 -> 시간 초과 
접근2: 누적 합
풀이: 
    //1.입력
    //2.완탐: 구간합 구하기
    //3.출력:
*/
int n;
int a[100010];
int sum[100010];
long long resSum = 0;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    //1.입력
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    //2.완탐: 구간합 구하기
    for (int i = 1; i <= n; i++) {
        sum[i] = sum[i - 1] + a[i];
    }
    for (int i = 1; i < n; i++) {
        resSum += a[i] * (sum[n] - sum[i]);
        //cout << resSum << '\n';
    }
    //3.출력:
    cout << resSum << '\n';
}
반응형