C - 駐車場の車庫入れ / Parking Lot Guidance Editorial by admin
GPT 5.2 HighOverview
Each car parks at “the smallest available parking space that is at least \(P_i\)” (or leaves if none exists). We simulate this in arrival order and count the number of cars that successfully park.
Analysis
The essence of this problem is, for each car: - Quickly find “the smallest numbered space that is at least \(P_i\) and still available” - Then mark that space as occupied.
Naive Approach
For each car \(i\), we could check \(P_i, P_i+1, \dots, M\) in order to find an empty space. In the worst case, this requires \(O(M)\) per car, giving \(O(NM)\) overall (which fits within this problem’s constraints, but we’ll consider a more general solution that works for larger constraints as well).
Key Insight
Once a space \(s\) is occupied, anyone who later starts searching from \(s\) only needs to look at \(s+1\) and beyond — there’s never a need to check \(s\) again. In other words, if we can maintain a “next candidate” pointer for each position — telling us where to look next when starting from a given number — we can speed things up significantly.
The classic technique to achieve this is managing “next available space” with Union-Find (Disjoint Set Union, DSU).
Algorithm
We use Union-Find as a data structure that returns “the next available parking space to check.”
- Treat
find(x)as a function that returns “the smallest available number that is at least \(x\)” - Initially, all spaces are available, so
parent[i] = i - When a car parks in space \(s\), that space can no longer be used, so we update
parent[s] = find(s+1)to set “the next candidate after \(s\) is \(s+1\)” (This way, any future call tofind(s)will skip \(s\) and proceed to \(s+1\) and beyond)
Using a Sentinel
Since find(M+1) might be called after space \(M\) is occupied, we prepare the array up to size M+2 and use M+1 as a sentinel indicating “no available space exists.”
Flow (Simulation)
For each car’s desired position \(p\):
1. Compute s = find(p) (the smallest available space that is at least \(p\))
2. If s <= M, parking succeeds:
- ans += 1
- Update parent[s] = find(s+1) to mark \(s\) as used
3. Otherwise (s == M+1), parking fails and the car leaves
Concrete Example
When \(M=5\) and desired positions are \([3,3,4,3]\):
- Car 1: find(3)=3 → parks at 3. From now on, find(3) returns 4
- Car 2: find(3)=4 → parks at 4. From now on, find(4) returns 5
- Car 3: find(4)=5 → parks at 5. From now on, find(5) returns 6 (sentinel)
- Car 4: find(3)=6 → exceeds \(M\), so cannot park
Complexity
- Time complexity: \(O\big((N+M)\alpha(M)\big)\) (where \(\alpha\) is the inverse Ackermann function, practically a constant)
- Space complexity: \(O(M)\)
Implementation Notes
Prepare
parentwith sizeM+2and useM+1as a sentinel (safe even whens+1equalsM+1)Apply path compression in
find(e.g.,parent[x] = parent[parent[x]]in the code) for speedupAfter parking, always update
parent[s] = find(s+1)to redirect to “the next available space” — this is the crucial stepSource Code
import sys
def main():
data = list(map(int, sys.stdin.read().split()))
if not data:
return
N, M = data[0], data[1]
P = data[2:2+N]
parent = list(range(M + 2)) # 1..M, sentinel M+1
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
ans = 0
for p in P:
s = find(p)
if s <= M:
ans += 1
parent[s] = find(s + 1)
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.