Problem list
Log in
#usaco0089 · USACO 2012-2013 March Bronze

Cow Race

Silver IV 2.0 s 256 MB

In order to finally settle their long-running dispute over who is the faster cow, Bessie and her friend Elsie decide to hold a race across the farm.

The two cows start at the same location and begin running in the same direction at the same time. The progress of each cow is described by a series of "segments", during each of which the cow runs at a constant speed. For example, Bessie might run at a speed of 5 for 3 units of time, then at a speed of 10 for 6 units of time. Bessie and Elsie both run for the same total amount of time.

The cows would like your help in counting the number of leadership changes during their race. A leadership change happens at a point in time when cow A pulls into the lead of cow B, whereas the last time one cow was in the lead, it was cow B. For example, if B is in the lead and then A pulls ahead, then this is a leadership change. If B is in the lead, and then A becomes even with B for some time and then finally pulls ahead, then this also counts as a leadership change.

Input

  • Line 1: Two space-separated integers, N and M. (1 <= N, M <= 1000)
  • Lines 2..1+N: Each line contains one of the N segments of Bessie's run, described by two integers: Bessie's speed and the amount of time she runs at that speed (both integers are in the range 1..1000).
  • Lines 2+N..1+N+M: Each line contains one of the M segments of Elsie's run, in the same format.

Output

The number of leadership changes during the race.

Example 1

Input
4 3
1 2
4 1
1 1
2 10
2 3
1 2
3 9
Output
2

Output details: Elsie is ahead until time t=3, when both cows meet. Bessie then pulls ahead briefly (first leadership change), only to be overtaken by Elsie (second leadership change).

Source
USACO 2012-2013 March Bronze
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code