Singing
SRM 653 · 2015-01-29 · by Chmel_Tolstiy
Problem Statement
Alice and Bob are going to sing a song together.
You are given three
You are also given a
Each note of the song must be sung by exactly one person: either Alice or Bob. Each person can only sing notes that are within their range. All notes with the same pitch must be sung by the same person. The number of times the singer changes during the song must be as small as possible.
Formally, a switch is a pair of consecutive notes that are sung by different persons. Compute and return the smallest possible number of switches when Alice and Bob sing the song.
Notes
- A singer is not required to sing a consecutive sequence of pitches. For example, sometimes Bob may sing pitches 1 and 3 while Alice sings pitches 2 and 4.
Constraints
- N will be between 1 and 1,000, inclusive.
- low will be between 1 and N, inclusive.
- high will be between low and N, inclusive.
- The number of elements in pitch will be between 1 and 1,000, inclusive.
- All elements of pitch will be between 1 and N.
3
2
2
{1,2,3,2,1,2}
Returns: 2
There are 3 pitches. Alice is able to sing pitches 2 and 3, while Bob is able to sing pitches 1 and 2. Thus, there are only two different ways in which they can sing the notes of the given song: Either they will sing in the order Bob-Alice-Alice-Alice-Bob-Alice (3 switches), or in the order Bob-Bob-Alice-Bob-Bob-Bob (only 2 switches). The second way of singing the song is better.
10
3
7
{4,4,5,5,6,5,3,6}
Returns: 0
Alice can sing the entire song. In that case there are no switches.
6
2
5
{5,3,1,6,4,2}
Returns: 1
In the optimal solution Bob sings the first three notes (pitches 5, 3, and 1), and Alice sings the next three notes.
10
4
5
{1,4,3,5,2,5,7,5,9}
Returns: 3
100
20
80
{2,27,3,53,53,52,52,60,85,89,100,53,60,2,3,53,100,89,40,42,2,53,2,85}
Returns: 5
Submissions are judged against all 117 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Singing with a public method int solve(int N, int low, int high, vector<int> pitch) · 117 test cases · 2 s / 256 MB per case