#P5269. Car Practice Distance Calculation
Car Practice Distance Calculation
Car Practice Distance Calculation
In this problem, you are given a car with \(N\) gears. Initially, at time \(0\), the gear is \(1\) and the engine speed (RPM) is \(L\). The RPM is constrained in the range \([L, R]\). Each second, two operations occur in sequence at the very beginning of the second:
- Gear Shift: You may change the gear by \(\pm1\) (use \(1\) for shifting up, \(-1\) for shifting down, and \(0\) for no gear change). When shifting up, the RPM resets to \(L\); when shifting down, it resets to \(R\).
- Acceleration: Optionally, you can press the accelerator. If you do, the RPM increases by \(X\) and is then capped at \(R\), i.e. it becomes \(\min(\text{rpm} + X, R)\).
After performing these operations, the car moves forward during that second by a distance equal to (current gear \(\times\) current RPM).
If the RPM equals \(R\) for \(K\) consecutive seconds, then at the end of the \(K\)th second, the engine stops immediately and no further operations are processed (even if any of these seconds involved a gear down operation that resets the RPM to \(R\)).
You are given the sequence of operations for \(T\) seconds. Your task is to simulate the process and calculate the total distance the car travels until the engine stops, or until all operations have been processed.
inputFormat
The first line contains six integers: \(N\), \(L\), \(R\), \(X\), \(K\), \(T\).
Then follow \(T\) lines, each containing two integers \(G\) and \(A\):
- \(G\) indicates the gear shift operation: \(1\) for shifting up, \(-1\) for shifting down, and \(0\) for no gear change.
- \(A\) is a flag indicating if the accelerator is pressed: \(1\) means pressed and \(0\) means not pressed.
It is guaranteed that all gear operations are valid so that the gear always remains in the range \([1, N]\).
outputFormat
Output a single integer representing the total distance traveled by the car.
sample
5 100 200 50 3 5
1 1
0 1
-1 0
0 1
0 0
1100