-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarn1.cpp
More file actions
53 lines (40 loc) · 1.03 KB
/
barn1.cpp
File metadata and controls
53 lines (40 loc) · 1.03 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
/*
ID: th3c0r11
TASK: barn1
LANG: C++14
*/
/* BTW: this is the first USACO problem i solved first try! */
#include <bits/stdc++.h>
int main()
{
std::ifstream in{"barn1.in"};
std::ofstream out{"barn1.out"};
int boards, stalls, cows;
in >> boards >> stalls >> cows;
in.ignore(100, '\n');
std::vector<int> occupiedStalls;
for (int i = 0; i < cows; ++i) {
std::string line;
std::getline(in, line);
occupiedStalls.push_back(std::atoi(line.data()));
}
std::sort(occupiedStalls.begin(), occupiedStalls.end());
// total number of stalls blocked (minimal)
if (boards >= cows) {
out << cows << '\n';
return 0;
}
std::vector<int> gapLengths;
for (int i = 1; i < occupiedStalls.size(); ++i)
gapLengths.push_back(occupiedStalls[i] - occupiedStalls[i - 1] - 1);
assert(gapLengths.size() == occupiedStalls.size() - 1);
std::sort(gapLengths.begin(), gapLengths.end());
// sort by length
int left = cows - boards;
int blocked = cows;
int i = 0;
while (left--) {
blocked += gapLengths[i++];
}
out << blocked << '\n';
}