From b8916e9ab73141ba62fae07578242acafe57f5d7 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sun, 19 Feb 2023 01:59:49 +0800 Subject: [PATCH] add: 230219-CN --- cpp/2302/230219-CN.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ cpp/2302/CMakeLists.txt | 2 +- cpp/CMakeLists.txt | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 cpp/2302/230219-CN.cpp diff --git a/cpp/2302/230219-CN.cpp b/cpp/2302/230219-CN.cpp new file mode 100644 index 0000000..476f4b1 --- /dev/null +++ b/cpp/2302/230219-CN.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +class Solution { +public: + static double maxAverageRatio(std::vector>& c, int ex) { + int n = c.size(); + double ans = std::transform_reduce( + std::execution::par, + c.begin(), + c.end(), + 0.0, + std::plus{}, + [](const std::vector& x) { + return double(x[0]) / double(x[1]); + } + ); + std::priority_queue> incr; + for (int i = 0; i < n; ++i) + incr.emplace(double(c[i][0] + 1) / double(c[i][1] + 1) - double(c[i][0]) / double(c[i][1]), i); + while (ex--) { + const auto [delta, i] = incr.top(); + incr.pop(); + ans += delta; + ++c[i][0], ++c[i][1]; + incr.emplace(double(c[i][0] + 1) / double(c[i][1] + 1) - double(c[i][0]) / double(c[i][1]), i); + } + return ans / n; + } +}; + +int main() { + std::vector> c { + {2,4},{3,9},{4,5},{2,10} + }; + std::printf("%.6f\n", Solution::maxAverageRatio(c, 4)); + return 0; +} diff --git a/cpp/2302/CMakeLists.txt b/cpp/2302/CMakeLists.txt index fcef160..e9f1b86 100644 --- a/cpp/2302/CMakeLists.txt +++ b/cpp/2302/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2302) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2302 230218-CN.cpp) +ADD_EXECUTABLE(2302 230219-CN.cpp) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 3415eac..9dbc513 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -16,7 +16,7 @@ ENDIF() # Optimisation # SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast -march=native -mtune=native -finline-functions -ffast-math -fomit-frame-pointer") -ADD_EXECUTABLE(leetcode-cpp main.cpp 2302/230218.cpp 2302/230218-CN.cpp) +ADD_EXECUTABLE(leetcode-cpp main.cpp 2302/230218.cpp 2302/230218-CN.cpp 2302/230219-CN.cpp) # ADD_SUBDIRECTORY(2112) # ADD_SUBDIRECTORY(2201)