forked from drken1215/book_algorithm_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_6_4.cpp
More file actions
39 lines (32 loc) · 999 Bytes
/
code_6_4.cpp
File metadata and controls
39 lines (32 loc) · 999 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm> // sort() や lower_bound() に必要です
using namespace std;
const int INF = 20000000; // 十分大きな値に
int main() {
// 入力を受け取る
int N, K;
cin >> N >> K;
vector<int> a(N), b(N);
for (int i = 0; i < N; ++i) cin >> a[i];
for (int i = 0; i < N; ++i) cin >> b[i];
// 暫定最小値を格納する変数
int min_value = INF;
// b をソート
sort(b.begin(), b.end());
// b に無限大を表す値 (INF) を追加しておく
// これを行うことで、iter = b.end() となる可能性を除外する
b.push_back(INF);
// a を固定して解く
for (int i = 0; i < N; ++i) {
// b の中で K - a[i] 以上の範囲での最小値を示すイテレータ
auto iter = lower_bound(b.begin(), b.end(), K - a[i]);
// イテレータの示す値を取り出す
int val = *iter;
// min_value と比較する
if (a[i] + val < min_value) {
min_value = a[i] + val;
}
}
cout << min_value << endl;
}