티스토리 뷰
백준 소스코드 [C++] 2143 두 배열의 합
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#include <sstream>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
int inDegree[10001], time[10001], accumulatedTime[10001];
queue<int> q;
vector<int> graph[10001];
void topologicalSort() {
while (q.empty()!=true) {
int here = q.front();
q.pop();
for (auto next : graph[here]) {
accumulatedTime[next] = max(accumulatedTime[next], accumulatedTime[here] + time[next]);
if (--inDegree[next] == 0) q.push(next);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;cin >> t;
int n, m;
cin >> n;
vector<int> a(n);
vector<int> sumA;
for (int i = 0;i < n;i++) cin >> a[i];
for (int i = 0;i < n;i++) {
int sum = 0;
for (int j = i;j < n;j++) {
sum += a[j];
sumA.push_back(sum);
}
}
cin >> m;
vector<int> b(m);
vector<int> sumB;
for (int i = 0;i < m;i++) cin >> b[i];
for (int i = 0;i < m;i++) {
int sum = 0;
for (int j = i;j < m;j++) {
sum += b[j];
sumB.push_back(sum);
}
}
sort(all(sumA)); sort(all(sumB));
int pA = 0, pB = (int)sumB.size() - 1;
ll res = 0;
while (pA <= (int)sumA.size() - 1 && 0 <= pB) {
if (sumA[pA] + sumB[pB] < t) pA++;
else if (sumA[pA] + sumB[pB] > t) pB--;
else {
ll cntA = 1, cntB = 1;
while (pA + 1 <= (int)sumA.size() - 1 && sumA[pA] == sumA[pA + 1])
{
cntA++, pA++;
}
while (0 <= pB - 1 && sumB[pB] == sumB[pB - 1])
{
cntB++, pB--;
}
res += cntA * cntB;
pA++, pB--;
}
}
cout << res;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 11780 플로이드 2 (0) | 2021.03.06 |
---|---|
백준 소스코드 [C++] 10159 저울 (0) | 2021.03.05 |
백준 소스코드 [C++] 2056 작업 (0) | 2021.03.03 |
백준 소스코드 [C++] 4056 스-스-스도쿠 (0) | 2021.03.02 |
백준 소스코드 [C++] 2239 스도쿠 (0) | 2021.03.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 최단경로 문제
- test coverage
- 강한 순환 참조
- 최단경로문제
- WWDC21
- MeTal
- WWDC19
- State Restoration
- 컴퓨터 추상화
- observeOn
- 다익스트라 시간복잡도
- 네트워크 플로우
- CompositionalLayout
- CPU와 Memory
- mach-o
- 코딩대회
- rxswift
- WWDC16
- WWDC17
- 최단경로 알고리즘
- 포드 풀커슨 알고리즘
- 부스트캠프 6기
- 최대 매칭
- HIG
- 네트워크 유량
- 에드몬드 카프 알고리즘
- IOS
- Testable
- 벨만포드 시간복잡도
- 벨만포드 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함