Array Division | CSES - Algoritmit ongelmanratkaisussa
https://cses.fi/alon/task/1085 #include #include #include using namespace std ; int main ( ) { ios_base :: sync_with_stdio ( false ) ; cin . tie ( nullptr ) ; int n , k ; cin >> n >> k ; vector arr ( n ) ; for ( int i = 0 ; i > arr [ i ] ; } long long lo = * max_element ( arr . begin ( ) , arr . end ( ) ) , hi = 0 ;
The problem at hand concerns the efficient division of an array into subarrays, with the objective of minimizing the maximum sum among those subarrays. This challenge is presented on the CSES platform, a renowned resource for algorithmic problem-solving. The source code provided implements a solution through binary search, a powerful technique for tackling such optimization problems.
To begin, the program initializes several variables and reads the input: an integer 'n' representing the size of the array, followed by 'k', the desired number of subarrays. Then, it populates a vector 'arr' with 'n' elements, each reflecting an integer value from the input. The next step involves determining the search bounds for the binary search algorithm.
The lower bound, 'lo', is assigned the maximum value within the array, while the upper bound, 'hi', is set to zero. If the array size 'n' equals 'k', the maximum element 'lo' is immediately printed as the answer and the program terminates.
For arrays where 'n' is not equal to 'k', the program proceeds to calculate the sum of all elements, storing it in 'hi'. If 'k' equals 1, indicating that the array should be left unsplit, the value of 'hi' is printed, and the program concludes. If neither of these conditions are met, the program enters a binary search loop. In each iteration, it calculates the midpoint 'mid' of the current search range defined by 'lo' and 'hi'.
Here, the goal is to ascertain if it's feasible to partition the array into 'k' or fewer subarrays, with each subarray's sum not exceeding 'mid'.
Within the binary search loop, a new variable 'currSum' is introduced to track the sum of elements within the current subarray being considered. Additionally, a counter 'subArrCnt' is initialized to 1, representing the number of subarrays formed thus far. The program then iterates through the array elements. For each element, it checks if adding it to 'currSum' would exceed 'mid'.
If so, it increments 'subArrCnt', resetting 'currSum' to zero, effectively marking the start of a new subarray. If the number of subarrays exceeds 'k' at any point, the loop breaks out early, as further refinement of 'mid' would not yield a valid solution.
Upon completing the iteration, the program assesses whether the number of subarrays 'subArrCnt' is less than or equal to 'k'. If true, it updates 'minSum' to 'mid' and narrows the search range by setting 'hi' to 'mid - 1', narrowing down to smaller potential values for the maximum subarray sum. Conversely, if 'subArrCnt' exceeds 'k', it implies that 'mid' is too small, and the search range is adjusted by setting 'lo' to 'mid + 1'.
This process repeats until 'lo' and 'hi' converge, at which point 'minSum' holds the smallest possible maximum subarray sum that satisfies the problem's constraints. Finally, the program outputs this value, providing the solution to the division problem.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to