add: 230313-CN

This commit is contained in:
Eatswap 2023-03-13 23:25:49 +08:00
parent 6c5916645b
commit 56ed525c17
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 36 additions and 1 deletions

35
cpp/2303/230313-CN.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
/**
* 2383. Minimum Hours of Training to Win a Competition
*
* You are entering a competition, and are given two positive integers initialEnergy and initialExperience denoting your initial energy and initial experience respectively.
* You are also given two 0-indexed integer arrays energy and experience, both of length n.
* You will face n opponents in order. The energy and experience of the ith opponent is denoted by energy[i] and experience[i] respectively. When you face an opponent, you need to have both strictly greater experience and energy to defeat them and move to the next opponent if available.
* Defeating the ith opponent increases your experience by experience[i], but decreases your energy by energy[i].
* Before starting the competition, you can train for some number of hours. After each hour of training, you can either choose to increase your initial experience by one, or increase your initial energy by one.
* Return the minimum number of training hours required to defeat all n opponents.
*/
class Solution {
public:
static int minNumberOfHours(int iEn, int iEx, const std::vector<int>& en, const std::vector<int>& ex) {
int ans = std::max(0, std::reduce(en.begin(), en.end(), 0) - iEn + 1);
for (int i : ex) {
if (i >= iEx) {
ans += i - iEx + 1;
iEx = i + 1;
}
iEx += i;
}
return ans;
}
};
int main() {
std::cout << Solution::minNumberOfHours(5, 3, {1,4,3,2}, {2,6,3,1});
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2303)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2303 230312.cpp) ADD_EXECUTABLE(2303 230313-CN.cpp)