-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookingaroom.cpp
73 lines (63 loc) · 1.89 KB
/
bookingaroom.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
/**
* @file bookingaroom.cpp
* @author William Weston
* @brief Booking a Room - Kattis Problem
* @version 0.1
* @date 2023-06-16
*
* @copyright Copyright (c) 2023
*
* Source: https://open.kattis.com/problems/bookingaroom
*/
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
auto find_available_room( std::vector<int> const& booked_rooms, int number_of_rooms ) -> int;
auto
main() -> int
{
int number_of_rooms;
int number_of_booked_rooms;
while ( std::cin >> number_of_rooms >> number_of_booked_rooms )
{
// Case 1: number_of_rooms == number_of_booked_rooms
if ( number_of_rooms == number_of_booked_rooms )
{
for ( auto i = 0; i < number_of_booked_rooms; ++i )
{
int room_number;
std::cin >> room_number;
}
std::cout << "too late\n";
}
// Case 2: number_of_booked_rooms < number_of_rooms
else
{
auto booked_rooms = std::vector<int>();
booked_rooms.reserve( number_of_booked_rooms );
for ( auto i = 0; i < number_of_booked_rooms; ++i )
{
int room_number;
std::cin >> room_number;
booked_rooms.push_back( room_number );
}
std::sort( booked_rooms.begin(), booked_rooms.end() );
auto const available_room = find_available_room( booked_rooms, number_of_rooms );
std::cout << available_room << '\n';
}
}
return EXIT_SUCCESS;
}
auto
find_available_room( std::vector<int> const& booked_rooms, int number_of_rooms ) -> int
{
for ( auto room_number = 1; room_number <= number_of_rooms; ++room_number )
{
// check if the room_number has been booked
auto const booked = std::binary_search( booked_rooms.begin(), booked_rooms.end(), room_number );
if ( !booked )
return room_number;
}
return -1; // no room found;
}