Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <vector>
4 : : #include <algorithm>
5 : : #include <numeric>
6 : : using namespace std;
7 : :
8 : 2 : int main() {
9 : 2 : ifstream inputFile("input.txt");
10 [ + + ]: 2 : if (!inputFile) {
11 : 1 : cerr << "Error: Could not open input file." << endl;
12 : 1 : return 1;
13 : : }
14 : :
15 : 1 : vector<int> list1;
16 : 1 : vector<int> list2;
17 : :
18 : : int num1;
19 : : int num2;
20 [ + + ]: 1001 : while (inputFile >> num1 >> num2) {
21 : 1000 : list1.push_back(num1);
22 : 1000 : list2.push_back(num2);
23 : : }
24 : :
25 : 1 : ranges::sort(list1);
26 : 1 : ranges::sort(list2);
27 : :
28 : : cout << inner_product(list1.begin(), list1.end(), list2.begin(), 0, plus<>(),
29 : 1001 : [](int a, int b) { return abs(a - b); })
30 : 1 : << endl;
31 [ + + ]: 3 : }
|