Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Don’t simulate the whole queue. Ask yourself: after everyone from the original 5 has drunk once, how many copies of each person are waiting?
The process happens in rounds. In round , each name appears time. In round , each name appears times. In round , each name appears times. You see the doubling nonsense now.
Round contains exactly cans, arranged as Sheldon repeated times, then Leonard repeated times, and so on.
Subtract whole rounds from until the remaining position lies inside the current round. Keep a variable , the number of consecutive times each name appears in that round.
Once is inside a round with block length , the answer is simply Use 1-indexed , so the is not optional. Off-by-one goblin defeated.
The queue looks chaotic if you stare at individual people. That’s bait. Don’t simulate the queue — it grows forever and can be as large as .
Instead, group the process into rounds.
Initially, the order is:
In the first round, each person drinks once:
After that, each person has been duplicated, so in the next round each name appears twice consecutively:
Then four times each:
And so on. In round , every name appears consecutive times, so the whole round contains:
cans.
Let be the number of consecutive copies of each name in the current round.
Start with:
The current round has:
positions. If is larger than that, the answer is not in this round, so subtract the whole round:
Then move to the next round:
Repeat until:
Now is the position inside the current round.
Inside a round with block size , the layout is:
So the zero-based index of the answer is:
The matters because cans are numbered from , but arrays are indexed from . Classic off-by-one trap. It wants your blood.
For :
So the answer is Sheldon.
Each iteration doubles , so there are only about iterations.
time and
extra memory.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll n;
cin >> n;
vector<string> names = {"Sheldon", "Leonard", "Penny", "Rajesh", "Howard"};
ll len = 1; // copies of each name in the current round
while (n > 5 * len) {
n -= 5 * len;
len *= 2;
}
cout << names[(n - 1) / len] << '\n';
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll n;
cin >> n;
vector<string> names = {"Sheldon", "Leonard", "Penny", "Rajesh", "Howard"};
ll len = 1; // copies of each name in the current round
while (n > 5 * len) {
n -= 5 * len;
len *= 2;
}
cout << names[(n - 1) / len] << '\n';
return 0;
}