O - 植物の水やり / Watering Plants Editorial by admin
gemini-3.5-flash-highOverview
This problem asks us to find the maximum number of seconds we can keep the water levels of all plants at \(1\) or higher, given that every second we can perform the operation “increase the water level of one plant by \(P\)” while the water levels of all plants decrease by \(1\).
A naive simulation will result in a TLE (Time Limit Exceeded) because the initial water levels \(T_i\) and the maximum seconds can be extremely large. This problem can be solved by formulating the “survival conditions” mathematically, classifying the time modulo \(P\), and then using binary search and a BIT (Binary Indexed Tree) for fast decision-making.
Analysis
1. Formulating the Survival Conditions
Let us consider the condition for all plants to survive (i.e., water levels are \(1\) or higher) at time \(t\). Let \(c_i\) be the number of times we water plant \(i\) up to time \(t\). The water level of plant \(i\) at time \(t\) is \(T_i + c_i P - t\). Since this must be at least \(1\), we have:
\[T_i + c_i P - t \ge 1 \iff c_i P \ge t - T_i + 1\]
Since \(c_i\) is a non-negative integer, the minimum number of waterings required for plant \(i\) is as follows:
\[c_i \ge \max\left(0, \left\lceil \frac{t - T_i + 1}{P} \right\rceil\right)\]
The total number of waterings we can perform up to time \(t\) is \(t\). Therefore, a necessary and sufficient condition to keep all plants alive up to time \(t\) is that for any time \(t' \le t\), the sum of the minimum required waterings is at most \(t'\).
\[F(t') = \sum_{i=1}^N \max\left(0, \left\lceil \frac{t' - T_i + 1}{P} \right\rceil\right) \le t' \quad (\text{for all } 0 \le t' \le t)\]
If we let \(t_{\min}\) be the minimum time at which this condition is violated (i.e., \(F(t) > t\)), the maximum survival time is \(t_{\min} - 1\).
2. Classification by the Remainder Modulo \(P\)
To find the minimum \(t\) such that \(F(t) > t\), we decompose the time \(t\) into its quotient \(q\) and remainder \(r\) when divided by \(P\):
\[t = q P + r \quad (0 \le r < P, \ q \ge 0)\]
Similarly, representing the initial value \(T_i\) of each plant as \(T_i = Q_i P + R_i\) (\(0 \le R_i < P\)), the numerator of the formula for the required waterings can be rewritten as:
\[t - T_i + 1 = (q - Q_i)P + r - R_i + 1\]
From this, the ceiling function part \(\lceil \dots \rceil\) can be simplified as follows:
\[\left\lceil \frac{(q - Q_i)P + r - R_i + 1}{P} \right\rceil = q - Q_i + \left\lceil \frac{r - R_i + 1}{P} \right\rceil\]
Here, \(\left\lceil \frac{r - R_i + 1}{P} \right\rceil\) becomes \(0\) or \(1\) depending on the relationship between \(r\) and \(R_i\) as follows: - When \(R_i \le r\): \(1\) - When \(R_i > r\): \(0\)
Using this, for each plant \(i\), we can express the required number of waterings as \(\max(0, q + 1 - th_i(r))\) using a certain threshold \(th_i(r)\). Specifically, the threshold \(th_i(r)\) is defined as follows:
\[th_i(r) = \begin{cases} Q_i & (R_i \le r) \\ Q_i + 1 & (R_i > r) \end{cases}\]
Therefore, for a fixed \(r\), the function \(F(q P + r)\) can be expressed in a simple form:
\[F(q P + r) = \sum_{i=1}^N \max(0, q + 1 - th_i(r))\]
3. Analyzing Behavior Based on the Relation Between \(N\) and \(P\)
Let \(f(q) = F(q P + r) - (q P + r)\), and we search for the minimum \(q \ge 0\) such that \(f(q) > 0\). When \(q\) is sufficiently large, we have \(q + 1 \ge th_i(r)\) for all \(i\), so the slope of the function converges to \(N - P\).
- When \(N < P\): Since the slope becomes negative, \(f(q) > 0\) will never hold for \(q \ge 1\). Thus, it is sufficient to check whether failure occurs only at \(q = 0\) (i.e., \(t = r\)).
- When \(N = P\): The slope converges to \(0\). In this case, if the value at infinity is positive, failure will eventually occur.
- When \(N > P\): Since the slope is positive (\(N - P > 0\)), failure is guaranteed to occur eventually. Since \(f(q)\) is a convex function (the slope is monotonically increasing), if \(f(0) \le 0\), we can find the boundary \(q\) where \(f(q) > 0\) using binary search.
Algorithm
Threshold Management and Coordinate Compression: The threshold \(th_i(r)\) of each plant takes the value of either \(Q_i\) or \(Q_i + 1\). We coordinate-compress these values and prepare two BITs (
bit_cnt,bit_sum) to manage their counts and sums.Iterating over the Remainder \(r\) and Updating Thresholds: Iterate \(r\) from \(0\) to \(P-1\). The moment \(r\) reaches \(R_i\) (\(r = R_i\)), the threshold of plant \(i\) decreases from \(Q_i + 1\) to \(Q_i\). Since this update (transition event) occurs at most once per plant, we can process it quickly by partitioning them into buckets beforehand.
Binary Search for the Minimum \(q\) (When \(N > P\)): For each \(r\), we find the minimum \(mid\) such that \(f(mid) > 0\) using binary search.
- Lower bound
low: Since at least \(P\) plants must have their thresholds less than or equal to \(mid\), we set the lower bound usingget_kth_val(P). - Upper bound
high: We take the minimum of the upper boundQ_limit(obtained from the region where the function increases linearly) and the upper bound calculated back from the minimum failure time \(t_{\min}\) found so far.
- Lower bound
Decision (Feasibility Check): Using BIT queries, we calculate \(\sum \max(0, mid + 1 - th_i(r))\) in \(O(\log N)\) time and proceed with the binary search.
Complexity
- Time Complexity: \(O(N \log N + P \log N \log(\max T / P))\)
- Coordinate compression and initial sorting take \(O(N \log N)\) time.
- The loop for \(r\) runs \(P\) times, and inside each loop, we perform a binary search at most \(\log(\max T / P)\) times. Since each step of the binary search calls a BIT query (\(O(\log N)\)), the entire binary search takes \(O(P \log N \log(\max T / P))\) time. This is sufficiently fast under the given constraints.
- Space Complexity: \(O(N + P)\)
- The memory required to manage the BITs and transition events is \(O(N + P)\).
Implementation Details
Binary Search on BIT (
get_kth_val): To retrieve the “\(k\)-th smallest value” from the multiset of thresholds in \(O(\log N)\) time, we implement binary lifting (binary search using powers of two) on the BIT structure. This significantly reduces the overall execution time.Floor Division (
floor_div): Since the/operator in C++ performs truncation toward zero, we prepare a helper function to correctly perform floor division when negative numbers are involved.Source Code
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
struct BIT {
int n;
vector<long long> tree;
BIT(int n) : n(n), tree(n + 1, 0) {}
void add(int i, long long delta) {
for (; i <= n; i += i & -i) tree[i] += delta;
}
long long query(int i) {
long long sum = 0;
for (; i > 0; i -= i & -i) sum += tree[i];
return sum;
}
};
long long floor_div(long long a, long long b) {
long long res = a / b;
long long rem = a % b;
if (rem != 0 && ((a < 0) ^ (b < 0))) {
res--;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
long long P;
if (!(cin >> N >> P)) return 0;
vector<long long> T(N);
vector<long long> vals;
vals.push_back(0);
for (int i = 0; i < N; ++i) {
cin >> T[i];
long long Q = T[i] / P;
vals.push_back(Q);
vals.push_back(Q + 1);
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
int V = vals.size();
BIT bit_cnt(V), bit_sum(V);
auto add_val = [&](long long val, long long delta) {
int idx = lower_bound(vals.begin(), vals.end(), val) - vals.begin() + 1;
bit_cnt.add(idx, delta);
bit_sum.add(idx, delta * val);
};
auto query_val = [&](long long q) -> pair<long long, long long> {
int idx = upper_bound(vals.begin(), vals.end(), q) - vals.begin();
if (idx == 0) return {0, 0};
return {bit_cnt.query(idx), bit_sum.query(idx)};
};
auto get_kth_val = [&](long long k) -> long long {
int idx = 0;
long long current_sum = 0;
int msk = 1;
while (msk <= V) msk <<= 1;
msk >>= 1;
for (int i = msk; i > 0; i >>= 1) {
if (idx + i <= V && current_sum + bit_cnt.tree[idx + i] < k) {
idx += i;
current_sum += bit_cnt.tree[idx];
}
}
return vals[idx];
};
vector<vector<int>> transition(P);
for (int i = 0; i < N; ++i) {
long long R = T[i] % P;
if (R > 0) {
transition[R].push_back(i);
}
}
for (int i = 0; i < N; ++i) {
long long Q = T[i] / P;
long long R = T[i] % P;
long long th = (R == 0) ? Q : Q + 1;
add_val(th, 1);
}
long long t_min = 3e18;
for (int r = 0; r < P; ++r) {
if (r > 0) {
for (int idx : transition[r]) {
long long Q = T[idx] / P;
add_val(Q + 1, -1);
add_val(Q, 1);
}
}
if (N < P) {
auto [cnt, sum] = query_val(0);
long long f = cnt - r;
if (f > 0) {
t_min = min(t_min, (long long)r);
break;
}
}
else if (N == P) {
auto [cnt, sum] = query_val(0);
long long f = cnt - r;
if (f > 0) {
t_min = min(t_min, (long long)r);
}
long long max_th = get_kth_val(N);
long long sum_all = bit_sum.query(V);
if (N - sum_all - r > 0) {
t_min = min(t_min, max_th * P + r);
}
}
else {
auto [cnt, sum] = query_val(0);
long long f = cnt - r;
if (f > 0) {
t_min = min(t_min, (long long)r);
}
long long q_turn = get_kth_val(P);
if (q_turn * P + r >= t_min) {
continue;
}
long long max_th = get_kth_val(N);
long long sum_all = bit_sum.query(V);
long long Q_limit = floor_div(sum_all + r - N, N - P) + 1;
Q_limit = max(Q_limit, max_th);
long long high = min(Q_limit, (t_min - r) / P);
long long low = q_turn;
long long ans_q = -1;
while (low <= high) {
long long mid = low + (high - low) / 2;
auto [c, s] = query_val(mid);
long long cur_f = (mid + 1) * c - s - mid * P - r;
if (cur_f > 0) {
ans_q = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
if (ans_q != -1) {
t_min = min(t_min, ans_q * P + r);
}
}
}
if (t_min >= 2e18) {
cout << -1 << "\n";
} else {
cout << t_min - 1 << "\n";
}
return 0;
}
This editorial was generated by gemini-3.5-flash-high.