Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions kangrae-jo/PrefixSum/31-kangrae-jo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int T, N, M;
vector<int> A, B;

void input(int& n, vector<int>& v) {
cin >> n;
v.assign(n, 0);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
}

vector<int> makeSub(int n, vector<int> v) {
vector<int> sub;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum += v[j];
sub.push_back(sum);
}
}
return sub;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);

// 0) input
cin >> T;
input(N, A);
input(M, B);

// 1) A, B λͺ¨λ“  λΆ€λΆ„ν•©
vector<int> subA = makeSub(N, A);
vector<int> subB = makeSub(M, B);

// 2) μ •λ ¬
sort(subA.begin(), subA.end());
sort(subB.begin(), subB.end());

// 3) νˆ¬ν¬μΈν„°λ‘œ 합이 T인 쌍 μ„ΈκΈ°
long long answer = 0;
int left = 0, right = subB.size() - 1;
while (left < subA.size() && 0 <= right) {
int sum = subA[left] + subB[right];
if (sum == T) {
long long countA = 1, countB = 1;
while (left + 1 < subA.size() && subA[left] == subA[left + 1]) {
countA++;
left++;
}
while (right - 1 >= 0 && subB[right] == subB[right - 1]) {
countB++;
right--;
}
answer += countA * countB;
left++;
right--;
}
else if (sum < T) left++;
else right--;
}

cout << answer << '\n';

return 0;
}
3 changes: 2 additions & 1 deletion kangrae-jo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
| 27μ°¨μ‹œ | 2024.06.01 | DFS | [개미꡴](https://www.acmicpc.net/problem/14725)|[#107](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/107)|
| 28μ°¨μ‹œ | 2024.06.10 | BFS | [경주둜 건섀](https://school.programmers.co.kr/learn/courses/30/lessons/67259)|[#110](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/110)|
| 29μ°¨μ‹œ | 2024.06.19 | TRIE | [[3μ°¨] μžλ™μ™„μ„±](https://school.programmers.co.kr/learn/courses/30/lessons/17685)|[#114](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/114)|
| 30μ°¨μ‹œ | 2024.07.18 | BFS | [치즈](https://www.acmicpc.net/problem/2638)|[#117](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/117)|
| 30μ°¨μ‹œ | 2024.07.18 | BFS | [치즈](https://www.acmicpc.net/problem/2638)|[#117](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/117)|
| 31μ°¨μ‹œ | 2024.07.31 | Prefix Sum | [두 λ°°μ—΄μ˜ ν•©](https://www.acmicpc.net/problem/2143)|[#122](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/122)|