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.
Fix the starting player first. The turn order is just the same circular order repeated over and over.
Think in laps around the table: during the -th lap, which players are still able to eat?
Player eats on lap exactly when . So the final lap that contains any eating is lap .
Only players with can eat on the final lap. Everyone else is already out of food, so they cannot possibly eat the last dish.
For any maximum player, choose the starting player to be immediately after them in the circle. Then that maximum player is last among all maximum players in the final lap.
Research check: the official Codeforces Round 1084 editorial code for 2200A and an independent write-up on cnblogs both reduce the problem to counting occurrences of the maximum value. The supplied statement matches that logic.
Let
Fix some starting player . The game can be viewed as repeated full laps around the table in the cyclic order
During the -th lap, player gets their -th turn. They eat on that turn if and only if they still have at least one dish left, which is exactly when
So the last lap where any eating can happen is lap . On that lap, the only players who can still eat are exactly the players with . Every player with fewer dishes is already empty. That kills the tempting but wrong idea of using total dishes modulo ; empty players still take turns and skip, so that shortcut faceplants.
Now we know the winner must be a maximum player. We also need to show every maximum player can win.
Take any player with . Choose the starting player to be the player immediately after in the circle, i.e.
In this cyclic order, player appears last. Therefore, on the final lap , all other maximum players eat their final dish before , all non-maximum players skip, and then eats the last remaining dish. So can be the winner.
Thus the answer is simply the number of indices such that
Edge cases are automatic: if , the answer is ; if all players have the same number of dishes, any player can be made last by choosing the right start, so the answer is .
Complexity per test case is 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();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
int mx = 0;
for (int &x : a) {
cin >> x;
mx = max(mx, x);
}
int ans = 0;
for (int x : a) {
ans += (x == mx);
}
cout << ans << '\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();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
int mx = 0;
for (int &x : a) {
cin >> x;
mx = max(mx, x);
}
int ans = 0;
for (int x : a) {
ans += (x == mx);
}
cout << ans << '\n';
}
return 0;
}