diff --git a/Development/Mate In One Move/CMakeLists.txt b/Development/Mate In One Move/CMakeLists.txt index e1ee0f9..2d16080 100644 --- a/Development/Mate In One Move/CMakeLists.txt +++ b/Development/Mate In One Move/CMakeLists.txt @@ -41,6 +41,7 @@ enable_testing() find_package( GTest REQUIRED ) +# change to target_sources set( TEST_SOURCE src/Solver.cpp tests/mate_in_one_move.t.cpp @@ -57,6 +58,7 @@ target_compile_options( ${TEST_SUITE} -pg -ftest-coverage -fsanitize=address + -fno-omit-frame-pointer -fsanitize=undefined -fsanitize=shift -fsanitize=shift-exponent @@ -74,11 +76,11 @@ target_compile_options( ${TEST_SUITE} -fsanitize=float-cast-overflow -fsanitize=enum -fsanitize=pointer-overflow - -fsanitize-address-use-after-scope + -fsanitize-address-use-after-scope -fstack-protector-all ) -target_link_options(${TEST_SUITE} +target_link_options( ${TEST_SUITE} PRIVATE -fsanitize=undefined -fsanitize=address @@ -91,4 +93,4 @@ target_include_directories( ${TEST_SUITE} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/in target_include_directories( ${TEST_SUITE} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src" ) include( GoogleTest ) -gtest_discover_tests( ${TEST_SUITE} ) \ No newline at end of file +gtest_discover_tests( ${TEST_SUITE} ) diff --git a/cookieselection.cpp b/cookieselection.cpp new file mode 100644 index 0000000..d5afbd1 --- /dev/null +++ b/cookieselection.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include + + + +class Holding_Area final +{ +public: + auto add( int diameter ) -> void; + auto request() -> int; + +private: + using max_heap = std::priority_queue, std::less>; + using min_heap = std::priority_queue, std::greater>; + + 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; +}