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.
A div. 1 round is always rated. A div. 2 round only cares about one thing: Vasya's rating before that round must be .
After a rated round, Vasya can choose any rating within distance . Since the only future condition is an upper bound, being lower is never worse.
So after every rated round, imagine Vasya drops his rating as much as possible: . There is no hidden benefit to staying high. High rating is just dead weight here.
When you see a 2, check the current minimum achievable rating. If it is , the round can be rated and you drop by . If it is , this round is unrated and the rating cannot change.
The whole solution is one greedy simulation: maintain the smallest rating Vasya can have before each round while achieving the maximum rated count so far.
We only need to track one number: the smallest rating Vasya can currently have while not sacrificing any rated rounds so far.
Call it .
There are two round types:
1: always rated.2: rated only if before the round.After a rated round, Vasya may choose his new rating anywhere from to , clipped at because rating cannot be negative.
The key observation is brutally simple: lower rating is always at least as good as higher rating.
Why? The only restriction in the future is for div. 2 rounds, and that restriction is:
There is never a round that requires Vasya to have rating at least something. So keeping rating high gives no advantage. It only makes future div. 2 rounds harder to enter. That's not strategy, that's self-sabotage with extra steps.
So after every rated round, we should choose the minimum possible next rating:
Now simulate the string.
For a div. 1 round:
It is always rated, so:
and then:
For a div. 2 round:
If , it is rated, so count it and drop the rating:
If , it is unrated. In an unrated round the rating cannot change, so we do nothing.
Why this greedy is correct
We can prove the invariant:
After processing any prefix of rounds, our simulated rating is the minimum possible rating among all strategies that achieve the maximum possible number of rated rounds in that prefix.
Initially this is true with .
When the next round is div. 1, every strategy gets this round rated, and choosing the lowest allowed new rating clearly keeps the invariant true.
When the next round is div. 2:
So the greedy never misses a rated round that could have been taken.
Complexity is just one pass over the string:
per test case, with 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--) {
ll R, X, D;
int n;
cin >> R >> X >> D >> n;
string s;
cin >> s;
int ans = 0;
for (char c : s) {
if (c == '1' || R < X) {
++ans;
R = max(0LL, R - D);
}
}
cout << ans << '\n';
}
}#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--) {
ll R, X, D;
int n;
cin >> R >> X >> D >> n;
string s;
cin >> s;
int ans = 0;
for (char c : s) {
if (c == '1' || R < X) {
++ans;
R = max(0LL, R - D);
}
}
cout << ans << '\n';
}
}