From b2430377a0b74d17f731728f0062082824438bf3 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Wed, 29 Mar 2023 17:09:20 +0800 Subject: [PATCH] add: 230329 --- cpp/2303/230329.cpp | 29 +++++++++++++++++++++++++++++ cpp/2303/CMakeLists.txt | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 cpp/2303/230329.cpp diff --git a/cpp/2303/230329.cpp b/cpp/2303/230329.cpp new file mode 100644 index 0000000..fa71c61 --- /dev/null +++ b/cpp/2303/230329.cpp @@ -0,0 +1,29 @@ +#include +#include + +/** + * 1402. Reducing Dishes + * + * A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time. + * Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i] * satisfaction[i]. + * Return the maximum sum of like-time coefficient that the chef can obtain after dishes preparation. + * Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value. + */ + +class Solution { +public: + static int maxSatisfaction(const std::vector& sa) { + const int n = sa.size(); + std::vector s(n); + std::partial_sort_copy(sa.begin(), sa.end(), s.begin(), s.end()); + int ans = std::max(0, s[n - 1]); + for (int i = n - 2, cur = s[n - 1]; i >= 0; --i) + ans = std::max(ans, cur += (s[i] += s[i + 1])); + return ans; + } +}; + +int main() { + Solution::maxSatisfaction({-1,-8,0,5,-9}); + return 0; +} diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 5c12928..97131c7 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230329-CN.cpp) +ADD_EXECUTABLE(2303 230329.cpp)