728x90
https://www.acmicpc.net/problem/2143
2143번: 두 배열의 합
첫째 줄에 T(-1,000,000,000 ≤ T ≤ 1,000,000,000)가 주어진다. 다음 줄에는 n(1 ≤ n ≤ 1,000)이 주어지고, 그 다음 줄에 n개의 정수로 A[1], …, A[n]이 주어진다. 다음 줄에는 m(1≤m≤1,000)이 주어지고, 그 다음 줄에 m개의 정수로 B[1], …, B[m]이 주어진다. 각각의 배열 원소는 절댓값이 1,000,000을 넘지 않는 정수이다.
www.acmicpc.net
배열별로 가질 수 있는 모든 부분합의 경우와 경우의 수를 우선 전부 구해 T가 되는 가지수를 구하면 되는 문제였다.
set를 활용해 key value 값으로 저장해 구현할수도 있지만 벡터를 활용해 해결하는 방법으로 풀어보았다.
lower bound upper bound에 대한 보다 자세한 설명은 이 분의 블로그 포스팅을 보면 될 것 같다.
https://blockdmask.tistory.com/168
[탐색] lower_bound, upper_bound
안녕하세요. BlockDMask 입니다. 오늘은 이진탐색과 유사하나 조금 다른 lower_bound 와 upper_bound에 대해 알아보겠습니다. 1. lower_bound lower_bound 란? - 이진탐색(Binary Search)기반의 탐색 방법입니다...
blockdmask.tistory.com
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long T;
int n,m;
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>T;
cin>>n;
vector<long long> A(n);
for(int i=0; i<n; i++)
cin>>A[i];
cin>>m;
vector<long long> B(m);
for(int i=0; i<m; i++)
cin>>B[i];
vector<long long> Asums,Bsums;
for(int i=0; i<n; i++){
long long sum = A[i];
Asums.push_back(sum);
for(int j=i+1; j<n; j++){
sum+=A[j];
Asums.push_back(sum);
}
}
for(int i=0; i<m; i++){
long long sum = B[i];
Bsums.push_back(sum);
for(int j=i+1; j<m; j++){
sum+=B[j];
Bsums.push_back(sum);
}
}
sort(Asums.begin(),Asums.end());
sort(Bsums.begin(),Bsums.end());
long long result=0;
for(int i=0; i<Asums.size(); i++){
int lowb=lower_bound(Bsums.begin(),Bsums.end(),T-Asums[i])-Bsums.begin();
int highb=upper_bound(Bsums.begin(),Bsums.end(),T-Asums[i])-Bsums.begin();
result += highb-lowb;
}
cout<<result;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'알고리즘 > 백준' 카테고리의 다른 글
백준 13335 트럭 - 큐 2개를 활용하자 c++ (0) | 2020.02.25 |
---|---|
백준 1261 알고스팟: bfs와 caching 쓰는 문제 c++ (0) | 2020.02.23 |
투 포인터 알고리즘, 슬라이딩 알고리즘 (0) | 2020.02.13 |
백준 1182 부분수열의 합- 재귀 브루트포스 (c++) (0) | 2020.02.13 |
백준 1987 알파벳 - dfs와 백트래킹 (c++) (0) | 2020.02.12 |