Maximizing Force on Bridge
생성일: 2024년 12월 29일
생성일: 2024년 12월 29일
Le Juste Pont – Qualification 2025
Joseph is two steps away from the fortress! He only needs to cross a bridge to reach the entrance. Unfortunately, some parts of the bridge are threatening to collapse due to its age. Joseph's spacesuit is very heavy, and this fatigues him. He wants to avoid getting exhausted to arrive at the fortress in good condition.
The bridge is N meters long and alternates between safe and fragile zones. The bridge is described by an array of 0s and 1s, one digit per meter. A 0 describes a position in a safe zone, and a 1 describes a position in a fragile zone. A safe zone is represented by one or more consecutive 0s, and a fragile zone by one or more consecutive 1s.
During his traversal, at each meter, Joseph Nageant can either move by walking or by swimming. Joseph Nageant must traverse the tunnel by swimming in all fragile zones to avoid causing the bridge to collapse. In safe zones, he can either pass by swimming or by walking.
Unlike a previous exercise, Joseph is not obliged to walk in every safe zone. He can choose to traverse a safe zone entirely by swimming.
However, Joseph has a certain endurance E ≥ 0 which allows him to swim for a maximum of E meters before having to start walking again. After swimming, he is obliged to walk for at least R meters. (Joseph cannot start a walking phase during the last R - 1 meters of the bridge. Furthermore, if Joseph starts to walk, he must walk for at least R meters before swimming again.)
The force of Joseph is the value of R - E. Help Joseph Nageant find a movement strategy that maximizes his force.
Input:
The input contains:
On the first line, an integer N, the length in meters of the bridge.
On the next line, a list of N integers separated by spaces, pont, the array representing the bridge. A 0 corresponds to a position in a safe zone, and a 1 corresponds to a position in a fragile zone.
Output:
Display the maximum force value that Joseph can obtain while crossing the bridge.
Constraints:
1 ≤ N ≤ 1000
pont<sub>i</sub> ∈ {0, 1}
There is always at least one safe zone.
Performance Constraints:
1 ≤ N ≤ 500 000
Execution Constraints:
Maximum memory usage: 100000 kilobytes
Maximum execution time: 500 milliseconds
To solve the problem of maximizing Joseph Nageant's force while crossing the bridge, we need to analyze the bridge's structure and determine the optimal strategy based on the constraints provided.
Bridge Representation:
0
s and 1
s.0
: Safe zone where Joseph can choose to walk or swim.1
: Fragile zone where Joseph must swim to avoid collapsing the bridge.Constraints:
Objective:
Minimum Endurance :
1
s (fragile zone) on the bridge.Maximum Recovery :
0
s) between fragile zones.Force Calculation:
Identify Safe and Fragile Zones:
0
s (safe zones) and 1
s (fragile zones).Determine and :
Compute the Force :
Below is the Python code that implements the above strategy:
pythondef max_force(N, pont): min_safe = float('inf') max_fragile = 0 i = 0 while i < N: current = pont[i] start = i while i < N and pont[i] == current: i += 1 length = i - start if current == 0: if length < min_safe: min_safe = length else: if length > max_fragile: max_fragile = length return min_safe - max_fragile # Read input import sys def main(): import sys input = sys.stdin.read data = input().split() N = int(data[0]) pont = list(map(int, data[1:N+1])) result = max_force(N, pont) print(result) if __name__ == "__main__": main()
Function max_force
:
N
: Length of the bridge.pont
: List representing the bridge.min_safe
: Stores the length of the smallest safe zone.max_fragile
: Stores the length of the largest fragile zone.min_safe
and max_fragile
accordingly.Reading Input:
N
, followed by N
integers representing the bridge.Output:
Example 1:
text5 0 1 0 1 0
Explanation:
Output:
0
Example 2:
text6 0 0 1 1 0 0
Explanation:
Output:
0
Example 3:
text9 0 1 1 1 0 0 1 1 0
Explanation:
Output:
-2
By identifying the smallest safe zone and the largest fragile zone, we can efficiently compute the maximum possible force that Joseph can achieve while crossing the bridge. This approach ensures that Joseph adheres to the constraints of swimming through fragile zones and managing his endurance and recovery optimally.