Peaks of a Musical Loop
Time limit2sMemory limit512 MB
Count local maxima and minima in a circular sequence of samples where consecutive values differ and the first and last differ.
- Level
Easy3 of 10
- Topics
- Implementation, Array, Simulation
- Solved
- No attempts yet
Problem
A musical loop is a piece of music written to repeat forever: it starts again every time it reaches the end, and no discontinuity is heard at the seam. Loops are common in game audio, especially in casual browser games.
One way to digitize a loop is PCM. PCM, or Pulse Code Modulation, is a technique for representing an analog signal, and it is widely used in digital audio. The technique samples the magnitude of the signal at regular time intervals and stores the sampled values in order. Playback reverses the process (demodulation).
Fernanda works at a game company. She composed a musical loop and encoded it in PCM. While looking at the waveform of her loop in an audio editor, she wanted to count its peaks. A peak of a waveform is a sample value that is a local maximum or a local minimum, that is, a point where the waveform turns around. The figure below shows (a) an example waveform and (b) the loop built from that waveform. The loop in (b) has 48 peaks.

Determine how many peaks Fernanda's musical loop has.
Input
The input contains several test cases. The first line of a test case contains an integer , the number of samples in the loop (). The second line contains integers separated by spaces, the magnitudes of the samples (, , and for every with ). When the loop plays, follows .
The end of the input is a line that contains only the number zero.
Output
For each test case, print one line with a single integer, the number of peaks in Fernanda's musical loop.