CSES - Array Division
Author: Michael Cao
In this problem, we're asked to divide an array into subarrays such that the maximum sum of a subarray is minimized.
Binary Search
Let's begin by making an important observation. First of all, if you can divide an array such that the maximum sum is at most , you can also divide the array such that the maximum sum is at most with the same division.
Greedy
Now, given some maximum sum , we can check whether a division is possible using a greedy algorithm. If we can divide the array into subarrays, then we can divide it into subarrays without increasing the maximum sum of a subarray. Therefore, we can greedily create subarrays as long as the sum of the subarray does not exceed , and check if the number of subarrays is .
Warning!
Make sure to use long long
s to avoid overflow! Implementing the greedy algorithm also requires some caution.
1#include <bits/stdc++.h>2using namespace std;34using ll = long long;56using vi = vector<int>;7#define pb push_back8#define rsz resize9#define all(x) begin(x), end(x)10#define sz(x) (int)(x).size()
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!