Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 문자열
- 우선순위 큐
- 트리
- BFS
- 브루트포스
- 누적 합
- DFS
- 백트래킹
- 구현
- 스택
- 투 포인터
- XR Interaction Toolkit
- Team Fortress 2
- 백준
- 유니티
- 자료구조
- 정렬
- Unreal Engine 5
- ue5
- 재귀
- 다이나믹 프로그래밍
- 시뮬레이션
- 유니온 파인드
- VR
- 알고리즘
- 다익스트라
- 그리디 알고리즘
- c++
- 수학
- 그래프
Archives
- Today
- Total
1일1알
백준 11660번 구간 합 구하기 5 C++ 본문
dp 테이블을 이용하여 누적 합을 저장해서 풀 수 있는 문제이다.
예제의 숫자들을 각 행마다 누적 합을 저장하여 저장한다.
(2,2)부터 (3,4) 까지의 합은
이 부분의 합을 구하면 되기 때문에
dp[2][4]-dp[2][2-1] + dp[3][4]-dp[3][2-1] = (14 - 2) + (18 - 3) = 27 이렇게 구할 수 있다.
이것을 코드로 나타내면
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <unordered_map>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, val, x1, x2, y1, y2;
cin >> n >> m;
vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> val;
dp[i][j] = dp[i][j - 1] + val;
}
}
while (m--) {
int sum = 0;
cin >> x1 >> y1 >> x2 >> y2;
for (int i = x1; i <= x2; i++) {
sum += dp[i][y2] - dp[i][y1 - 1];
}
cout << sum << "\n";
}
};
이런 식으로 코드를 짤 수 있다.
'알고리즘' 카테고리의 다른 글
백준 12851번 숨바꼭질2 C++ (0) | 2021.10.20 |
---|---|
백준 16953번 A->B (C++) (0) | 2021.10.19 |
백준 11725번 트리의 부모 찾기 C++ (0) | 2021.10.17 |
백준 9465번 스티커 C++ (0) | 2021.10.16 |
백준 4153번 직각삼각형 C++ (0) | 2021.10.15 |