-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathb.cpp
More file actions
72 lines (70 loc) · 1.46 KB
/
b.cpp
File metadata and controls
72 lines (70 loc) · 1.46 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
long long n, m, p;
int k;
vector<long long> a, b;
void printResult() { // hàm dùng để in một cấu hình ra ngoài
for(int i = 1 ; i <=k ; i++) {
cout<<b[i]<<" " ;
}
cout<<endl ;
}
void backtrack(int i)
{ // hàm quay lui
for (int j =b[i - 1] + 1; j <= m - k + i; j++)
{ // xét các khả năng của j
b[i] = j; // ghi nhận một giá trị của j
if (i == k)
{ // nếu cấu hình đã đủ k phần tử
// in một cấu hình ra ngoài
printResult();
}
else
{
backtrack(i + 1); // quay lui
}
}
}
void toHop()
{ // hàm liệt kê các tổ hợp
if (k >= 0 && k <= m)
{
a[0] = 0; // khởi tạo giá trị a[0]
backtrack(1);
}
else
{
}
}
int main()
{
freopen("b.inp", "r", stdin);
freopen("b.out", "w", stdout);
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
{
long long x;
cin >> x;
a.push_back(x);
}
for (int i = 0; i < m; i++)
{
long long x;
cin >> x;
b.push_back(x);
}
toHop();
long long ans = 0;
for (int i = 0; i < n; i++)
{
long long min = 0;
for (int j = 0; j < m; j++)
{
if (abs(a[i] - b[j]) < min)
min = abs(a[i] - b[j]);
}
ans += min;
}
}