-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.cpp
More file actions
49 lines (47 loc) · 1.1 KB
/
day1.cpp
File metadata and controls
49 lines (47 loc) · 1.1 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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <cmath>
#include <limits>
using namespace std;
int main()
{
// const int MAX_VALUE = 99999;
// vector<int> freq_left(MAX_VALUE + 1, 0), freq_right(MAX_VALUE + 1, 0);
vector<int> left, right;
ifstream input_file("input.txt");
string line;
int m = -1;
while (getline(input_file, line))
{
stringstream ss(line);
int l, r;
ss >> l >> r;
left.push_back(l);
right.push_back(r);
m = max(m, l);
// freq_left[l]++;
// freq_right[r]++;
}
input_file.close();
// int ans = 0;
// int carry_left = 0, carry_right = 0;
// for (int i = 0; i <= MAX_VALUE; i++)
// {
// carry_left += freq_left[i];
// carry_right += freq_right[i];
// int match = min(carry_left, carry_right);
// ans += match * i;
// carry_left -= match;
// carry_right -= match;
// }
sort(left.begin(), left.end());
sort(right.begin(), right.end());
int ans = 0;
for (int i = 0; i < left.size(); i++)
ans += abs(left[i]-right[i]);
cout << ans << endl;
cout << m << endl;
return 0;
}