#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
// naive approach
int waitingTimeSim(const std::vector<int> &tickets, int p)
{
// setup sim. model
std::queue<std::pair<int, bool> > people;
for (int i = 0, n = (int)tickets.size(); i < n; ++i) {
people.push(std::make_pair(tickets[i], i == p));
}
// simulation
int tP = 0;
for (;;) {
std::pair<int, bool> person = people.front();
people.pop();
--person.first; ++tP; // by ticket
if (!person.first) { // if person is done
if (person.second) return tP; // It's Jesse -> terminate
} else people.push(person);
}
}
// analytical approach
int waitingTime(const std::vector<int> &tickets, int p)
{
int tP = 0, ticketsP = tickets[p];
for (int i = 0, n = (int)tickets.size(); i < n; ++i) {
tP += std::min(tickets[i], ticketsP - (i > p));
// i > p -> people after jesse -> decr. by 1
}
return tP;
}
int main()
{
{ std::vector<int> tickets{ 2, 6, 3, 4, 5 };
for (int p = 0, n = tickets.size(); p < n; ++p) {
std::cout << "tickets{ 2, 6, 3, 4, 5 }, p = " << p << std::endl;
int tS = waitingTimeSim(tickets, p);
std::cout << "simulated t: " << tS << std::endl;
int t = waitingTime(tickets, p);
std::cout << "computed t: " << t << std::endl;
}
}
{ std::vector<int> tickets{ 5, 5, 2, 3 };
for (int p = 0, n = tickets.size(); p < n; ++p) {
std::cout << "tickets{ 5, 5, 2, 3 }, p = " << p << std::endl;
int tS = waitingTimeSim(tickets, p);
std::cout << "simulated t: " << tS << std::endl;
int t = waitingTime(tickets, p);
std::cout << "computed t: " << t << std::endl;
}
}
return 0;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDx2ZWN0b3I+CgovLyBuYWl2ZSBhcHByb2FjaAoKaW50IHdhaXRpbmdUaW1lU2ltKGNvbnN0IHN0ZDo6dmVjdG9yPGludD4gJnRpY2tldHMsIGludCBwKQp7CiAgLy8gc2V0dXAgc2ltLiBtb2RlbAogIHN0ZDo6cXVldWU8c3RkOjpwYWlyPGludCw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UoKTsgcCA8IG47ICsrcCkgewogICAgICBzdGQ6OmNvdXQgPDwgInRpY2tldHN7IDIsIDYsIDMsIDQsIDUgfSwgcCA9ICIgPDwgcCA8PCBzdGQ6OmVuZGw7CiAgICAgIGludCB0UyA9IHdhaXRpbmdUaW1lU2ltKHRpY2tldHMsIHApOwogICAgICBzdGQ6OmNvdXQgPDwgInNpbXVsYXRlZCB0OiAiIDw8IHRTIDw8IHN0ZDo6ZW5kbDsKICAgICAgaW50IHQgPSB3YWl0aW5nVGltZSh0aWNrZXRzLCBwKTsKICAgICAgc3RkOjpjb3V0IDw8ICJjb21wdXRlZCB0OiAgIiA8PCB0IDw8IHN0ZDo6ZW5kbDsKICAgIH0KICB9CiAgeyBzdGQ6OnZlY3RvcjxpbnQ+IHRpY2tldHN7IDUsIDUsIDIsIDMgfTsKICAgIGZvciAoaW50IHAgPSAwLCBuID0gdGlja2V0cy5zaXplKCk7IHAgPCBuOyArK3ApIHsKICAgICAgc3RkOjpjb3V0IDw8ICJ0aWNrZXRzeyA1LCA1LCAyLCAzIH0sIHAgPSAiIDw8IHAgPDwgc3RkOjplbmRsOwogICAgICBpbnQgdFMgPSB3YWl0aW5nVGltZVNpbSh0aWNrZXRzLCBwKTsKICAgICAgc3RkOjpjb3V0IDw8ICJzaW11bGF0ZWQgdDogIiA8PCB0UyA8PCBzdGQ6OmVuZGw7CiAgICAgIGludCB0ID0gd2FpdGluZ1RpbWUodGlja2V0cywgcCk7CiAgICAgIHN0ZDo6Y291dCA8PCAiY29tcHV0ZWQgdDogICIgPDwgdCA8PCBzdGQ6OmVuZGw7CiAgICB9CiAgfQogIHJldHVybiAwOwp9
tickets{ 2, 6, 3, 4, 5 }, p = 0
simulated t: 6
computed t: 6
tickets{ 2, 6, 3, 4, 5 }, p = 1
simulated t: 20
computed t: 20
tickets{ 2, 6, 3, 4, 5 }, p = 2
simulated t: 12
computed t: 12
tickets{ 2, 6, 3, 4, 5 }, p = 3
simulated t: 16
computed t: 16
tickets{ 2, 6, 3, 4, 5 }, p = 4
simulated t: 19
computed t: 19
tickets{ 5, 5, 2, 3 }, p = 0
simulated t: 14
computed t: 14
tickets{ 5, 5, 2, 3 }, p = 1
simulated t: 15
computed t: 15
tickets{ 5, 5, 2, 3 }, p = 2
simulated t: 7
computed t: 7
tickets{ 5, 5, 2, 3 }, p = 3
simulated t: 11
computed t: 11