Problem 467
Car Fleet
Medium · 20 pts · Array, Stack, Sorting
Problem
N cars drive along a one-lane road towards a target at position T. Each car starts at a distinct position and drives at its own constant speed. A car can never pass another car: when it catches up with a slower car ahead, it slows down and they drive together as one fleet. A car that catches up exactly at the target also joins that fleet. Count the fleets that arrive at the target. Sort the cars from closest to farthest from the target and compare their arrival times.
Input
First line: N and T. Second line: the N starting positions. Third line: the N speeds.
Output
Print the number of car fleets.
Constraints
1 <= N <= 100000; 0 < T <= 1000000; 0 <= position < T; positions are distinct; 1 <= speed <= 1000000
Example input
5 12 10 8 0 5 3 2 4 1 1 3
Example output
3
Explanation
With T = 12, the cars at 10 and 8 both arrive at time 1, forming one fleet; the car at 0 arrives alone; and the car at 3 (speed 3) catches the car at 5 (speed 1) at position 6, after which they drive on together and arrive at time 7. That is 3 fleets.