-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookieselection.cpp
81 lines (68 loc) · 1.52 KB
/
cookieselection.cpp
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
73
74
75
76
77
78
79
80
81
#include <cassert>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
class Holding_Area final
{
public:
auto add( int diameter ) -> void;
auto request() -> int;
private:
using max_heap = std::priority_queue<int, std::vector<int>, std::less<int>>;
using min_heap = std::priority_queue<int, std::vector<int>, std::greater<int>>;
max_heap max_queue = {};
min_heap min_queue = {};
};
auto
Holding_Area::add( int diameter ) -> void
{
if ( max_queue.empty() || diameter >= max_queue.top() )
{
min_queue.push( diameter );
}
else
{
max_queue.push( diameter );
min_queue.push( max_queue.top() );
max_queue.pop();
}
if ( min_queue.size() > ( max_queue.size() + 1 ) )
{
max_queue.push( min_queue.top() );
min_queue.pop();
}
}
auto
Holding_Area::request() -> int
{
assert( min_queue.size() );
auto cookie = min_queue.top();
min_queue.pop();
if ( max_queue.size() > min_queue.size() )
{
min_queue.push( max_queue.top() );
max_queue.pop();
}
return cookie;
}
auto
main() -> int
{
auto holding_area = Holding_Area{};
for ( std::string input; std::cin >> input; )
{
if ( input == "#" ) // request
{
std::cout << holding_area.request() << '\n';
}
else // cookie arrival
{
int const diameter = std::stoi( input );
holding_area.add( diameter );
}
}
return EXIT_SUCCESS;
}