UVa 13190 – Rockabye Tobby
"Rockabye baby, don’t you cry." Tobby is very good at playing catch, and he likes this game. One day, he decided to go out and play, even though it was raining at the time. He played for a long time, and besides playing catch many times, he also got sick on the side, which was quite embarrassing and sad. That's why his mother (Big Doggie) is taking care of him now. In addition to singing that beautiful lullaby (Rockabye), she also prepares medicine for him at specified times. The doctor's prescription specifies the name of the medicine and how often to take it. The doctor told him that if he followed the prescription and took K medicines, he would get better. Tobby doesn't like being sick (actually, anyone does), so he assured his mother that he would take the medicine on time. That's why he wants to know now which K medicines he must take first to get better and continue playing catch on rainy days. Can you help him?
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
For each case, the output must have K lines, each in the form "t m". "t m" indicates that Tobby must take medication m at time t. If Tobby must take two or more medications at the same time t, the output should be based on their priority. | 對於每個 Case,輸出必須有k行,每行形式為「t m」。 「t m」表示在 t 時刻 Tobby 必須服用藥物 m。 如果必須在同一時間 t 服用兩種或兩種以上藥物,則應根據其優先級進行輸出). |
1 2 5 Acetaminophen 20 Loratadine 30 | 20 Acetaminophen 30 Loratadine 40 Acetaminophen 60 Acetaminophen 60 Loratadine |
Thought Process
Use a vector of pairs to store the names and frequencies of the medications. Then, use a while loop to check if the current time matches any medication that needs to be taken (i.e. if it is divisible by the frequency). It's important to check if the number of medications taken is equal to K after taking a medication. If it is, break the loop.
Sample Code-ZeroJudge E520: Rockabye Tobby
#include <iostream>
#include <vector>
using namespace std;
pair<string, int> rtn (string name, int time)
{
pair<string, int>tmp;
tmp.first = name;
tmp.second = time;
return tmp;
}
int main() {
int N;
cin >> N;
for (int i = 0; i<N; i++)
{
int med, K;
cin >> med >> K;
vector<pair<string, int>>v;
for (int j = 0; j<med; j++)
{
string name;
int time;
cin >> name >> time;
v.push_back(rtn(name, time));
}
int count = 0, j = 1;
while (count < K)
{
for (auto it:v)
{
if (j % it.second == 0)
{
cout << j << " " << it.first << "\n";
count++;
if (count >= K) break;
}
}
j++;
}
}
}
//ZeroJudge E520
//Dr. SeanXD