O - 植物の水やり / Watering Plants Editorial by admin
gpt-5.5-highOverview
We can think of watering each plant as a “job with a deadline.”
We want to find the first time \(x\) at which the “number of waterings required by time \(x\)” exceeds the “number of operations \(x\) that can be performed by time \(x\)”. The moment just before this time will be our answer.
Observation
Consider the time \(x\), which is immediately after performing \(x\) operations.
If we have watered plant \(i\) a total of \(w_i(x)\) times up to this point, its water level is:
\(T_i + P w_i(x) - x\)
Since this value must be at least \(1\), we require:
\(T_i + P w_i(x) - x \ge 1\)
which means
\(w_i(x) \ge \left\lceil \frac{x - T_i + 1}{P} \right\rceil\)
is required.
For plant \(i\), this can be interpreted as:
- The deadline for the 1st watering is \(T_i\)
- The deadline for the 2nd watering is \(T_i + P\)
- The deadline for the 3rd watering is \(T_i + 2P\)
and so on.
Therefore, we can view each plant as generating an infinite sequence of jobs with deadlines:
\(T_i,\ T_i+P,\ T_i+2P,\ \ldots\)
Let \(D(x)\) be the number of jobs whose deadlines are at or before time \(x\).
Since we can perform at most one watering per second, the number of jobs we can complete by time \(x\) is at most \(x\).
Thus, a necessary condition is that
\(D(x) \le x\)
must hold for all \(x\).
Furthermore, since we can always process jobs in increasing order of their deadlines, this condition is also sufficient.
In other words, if we find the first time \(x\) such that
\(D(x) > x\)
then we cannot maintain the plants at that moment, so the answer is \(x-1\).
Here, let
\(A(x) = D(x) - x\)
We want to find the first time \(x\) such that \(A(x) > 0\).
Let’s consider the difference \(A(x) - A(x-1)\):
\(A(x) - A(x-1)\)
This is equal to:
- The number of jobs with a deadline at time \(x\)
- minus \(1\) (the capacity of operations we can perform at that second).
The plants that have a deadline at time \(x\) are those satisfying:
\(T_i \le x\) and \(T_i \equiv x \pmod P\)
Therefore,
\(A(x) - A(x-1) = \#\{i \mid T_i \le x,\ T_i \equiv x \pmod P\} - 1\)
The key observation is that this value depends only on \(x \bmod P\).
However, plant \(i\) does not join the deadline sequence until time \(T_i\). Thus, we should process the plants in ascending order of \(T_i\) and gradually add participating plants.
A naive simulation second-by-second is too slow because the answer can be extremely large.
Also, simply checking if \(D(k) \le k\) for some specific time \(k\) is insufficient.
For example, if \(D(x) > x\) holds at any intermediate point, the plants will wither at that moment.
Even if we have extra capacity later, it is already too late.
Therefore, we need to efficiently maintain the prefix maximums (i.e., the maximum value at any point along the way).
Algorithm
We maintain the differences at each time step:
\(A(x) - A(x-1)\)
First, sort the initial water levels \(T_i\) of the plants in ascending order.
For the currently participating plants, we maintain the count for each remainder \(r\):
\(\text{cnt}[r] = \#\{i \mid T_i \le \text{current time},\ T_i \equiv r \pmod P\}\)
Then, the difference at time \(x\) is:
\(\text{cnt}[x \bmod P] - 1\)
We manage this periodic sequence as:
\(B_r = \text{cnt}[r] - 1\)
If no new plants join during the interval \([L, R]\), the difference sequence repeats with a period of \(P\).
For this interval, we need the following two values:
- The total increase of \(A\) over the entire interval
- The maximum prefix sum within the interval
If the current value is \(a = A(\text{current time})\), then if
\(a + \text{prefix\_max} > 0\)
holds, then the plants become unsustainable for the first time within this interval.
To efficiently query the following for the periodic sequence \(B\):
- Interval sum
- Prefix maximum
we use a segment tree.
Each node in the segment tree stores:
sum: the sum of the intervalmax: the maximum prefix sum of the interval
To handle intervals that wrap around modulo \(P\), we manage the sequence as an array of length \(2P\) (by concatenating the array of length \(P\) with itself).
When processing an interval of length len, we decompose it as:
\(\text{len} = qP + rem\)
Let \(S\) be the sum of one full period.
- The sum over \(q\) full periods is \(qS\).
- The remaining part of length \(rem\) is queried from the segment tree.
As for the prefix maximum:
- If \(S > 0\), the values increase in subsequent periods.
- If \(S \le 0\), we only need to check the first period.
We can utilize these properties.
The flow of the algorithm is as follows:
- Sort \(T_i\) in ascending order.
- Start with a state where no plants have joined yet.
- Process plants with the same \(T_i\) together.
- Check if \(A(x) > 0\) occurs in the interval up to just before the next \(T_i\).
- If it does not occur, add the plants at that time to their respective remainders \(T_i \bmod P\).
- After all plants have been added, the periodic sequence becomes fixed.
- If the sum of one period is positive, the system will eventually fail, so we find that failure time.
- If the sum of one period is at most \(0\), we only need to check the first period. If it does not fail there, it can be maintained indefinitely.
If we define the first failing time as bad_time, then the answer is:
\(\text{bad\_time} - 1\)
Complexity
- Time Complexity: \(O(N \log N + N \log P)\)
- Space Complexity: \(O(N + P)\)
Sorting \(T_i\) takes \(O(N \log N)\) time, and updating and querying the segment tree for each distinct \(T_i\) takes \(O(\log P)\) time.
Implementation Details
The failure condition is \(A(x) > 0\). If \(A(x) = 0\), we are just on time, so it is safe.
Since the first deadline for plant \(i\) occurs exactly at time \(T_i\), plants with the same \(T_i\) should be added together.
The sum of one period is managed as
period_sum. If the number of currently participating plants is \(M\), this sum is \(M - P\).Since the answer can be extremely large, you must use 64-bit integers (
long longin C++) for implementation. In Python, this is not an issue as integers have arbitrary precision.Source Code
import sys
NEG = -10**30
class SegTree:
def __init__(self, p):
self.p = p
n = 2 * p
size = 1
while size < n:
size <<= 1
self.size = size
self.sumv = [0] * (2 * size)
self.maxv = [NEG] * (2 * size)
base = size
for i in range(n):
self.sumv[base + i] = -1
self.maxv[base + i] = -1
for i in range(size - 1, 0, -1):
ls = self.sumv[i << 1]
rs = self.sumv[i << 1 | 1]
lm = self.maxv[i << 1]
rm = self.maxv[i << 1 | 1]
self.sumv[i] = ls + rs
v = ls + rm
self.maxv[i] = lm if lm >= v else v
def add(self, pos, delta):
p = self.p
size = self.size
sumv = self.sumv
maxv = self.maxv
for x in (pos, pos + p):
k = size + x
nv = sumv[k] + delta
sumv[k] = nv
maxv[k] = nv
k >>= 1
while k:
lc = k << 1
rc = lc | 1
ls = sumv[lc]
rs = sumv[rc]
lm = maxv[lc]
rm = maxv[rc]
sumv[k] = ls + rs
v = ls + rm
maxv[k] = lm if lm >= v else v
k >>= 1
def query(self, l, r):
size = self.size
sumv = self.sumv
maxv = self.maxv
l += size
r += size
lsum = 0
rsum = 0
lmax = NEG
rmax = NEG
while l < r:
if l & 1:
sm = sumv[l]
mx = maxv[l]
v = lsum + mx
if v > lmax:
lmax = v
lsum += sm
l += 1
if r & 1:
r -= 1
sm = sumv[r]
mx = maxv[r]
v = sm + rmax
rmax = mx if mx >= v else v
rsum = sm + rsum
l >>= 1
r >>= 1
v = lsum + rmax
m = lmax if lmax >= v else v
return lsum + rsum, m
def find_first(self, l, r, threshold):
size = self.size
sumv = self.sumv
maxv = self.maxv
l += size
r += size
left = []
right = []
while l < r:
if l & 1:
left.append(l)
l += 1
if r & 1:
r -= 1
right.append(r)
l >>= 1
r >>= 1
acc = 0
skipped = 0
for node in left + right[::-1]:
if acc + maxv[node] > threshold:
length = size >> (node.bit_length() - 1)
offset = 0
while node < size:
lc = node << 1
half = length >> 1
if acc + maxv[lc] > threshold:
node = lc
length = half
else:
acc += sumv[lc]
offset += half
node = lc | 1
length = half
return skipped + offset + 1
acc += sumv[node]
skipped += size >> (node.bit_length() - 1)
return None
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
n = data[0]
p = data[1]
t = data[2:]
t.sort()
seg = SegTree(p)
query = seg.query
find_first = seg.find_first
period_sum = -p
def interval_info(start, length, ssum):
st = start % p
q, rem = divmod(length, p)
total = q * ssum
maxpref = NEG
if q:
_, m = query(st, st + p)
if ssum > 0:
mf = m + (q - 1) * ssum
else:
mf = m
maxpref = mf
if rem:
rs, rm = query(st, st + rem)
v = q * ssum + rm
if v > maxpref:
maxpref = v
total += rs
return total, maxpref
def find_cross(start, length, current, ssum):
threshold = -current
st = start % p
q, rem = divmod(length, p)
if q:
_, m = query(st, st + p)
if ssum > 0:
if m > threshold:
cyc = 0
else:
cyc = (threshold - m) // ssum + 1
if cyc < q:
need = threshold - cyc * ssum
inside = find_first(st, st + p, need)
return cyc * p + inside
else:
if m > threshold:
return find_first(st, st + p, threshold)
if rem:
_, rm = query(st, st + rem)
base = q * ssum
if base + rm > threshold:
inside = find_first(st, st + rem, threshold - base)
return q * p + inside
return None
def find_cross_inf(start, current, ssum):
threshold = -current
st = start % p
_, m = query(st, st + p)
if m > threshold:
cyc = 0
else:
cyc = (threshold - m) // ssum + 1
need = threshold - cyc * ssum
inside = find_first(st, st + p, need)
return cyc * p + inside
cur = 0
a = 0
i = 0
while i < n:
e = t[i]
length = e - cur - 1
if length > 0:
total, maxpref = interval_info(cur + 1, length, period_sum)
if a + maxpref > 0:
off = find_cross(cur + 1, length, a, period_sum)
bad_time = cur + off
print(bad_time - 1)
return
a += total
cur += length
j = i + 1
while j < n and t[j] == e:
j += 1
cnt = j - i
seg.add(e % p, cnt)
period_sum += cnt
i = j
start = cur + 1
if period_sum > 0:
off = find_cross_inf(start, a, period_sum)
bad_time = start + off - 1
print(bad_time - 1)
else:
_, maxpref = interval_info(start, p, period_sum)
if a + maxpref > 0:
off = find_cross(start, p, a, period_sum)
bad_time = start + off - 1
print(bad_time - 1)
else:
print(-1)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.5-high.