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.
Think of each board as either removed or left standing. The bad thing is a run of at least removed boards.
To maximize removed boards, you want to leave as few boards as possible, but every long enough stretch of removed boards needs to be interrupted.
Look at disjoint groups of boards: boards , then , and so on. In each full group, can all be removed?
No full group of boards can be entirely removed, because that would already create an escape hole. So each full group forces at least one board to stay.
There are full groups of size . Leave exactly one board in each of them, for example boards , and remove everything else.
We need choose as many removed boards as possible while avoiding consecutive removed boards.
Call a removed board 1 and a board left standing 0. The condition is simple: the binary string of length must not contain consecutive 1s. So the whole problem is: maximize the number of 1s without making a run of length .
First, get a hard upper bound. Split the first boards into disjoint blocks of length :
Inside any full block of consecutive boards, we cannot remove all boards. If we did, the mower has a hole exactly meters wide, and it's gone. So every full block must contain at least one board that remains.
There are
such full blocks, so at least that many boards must stay. Therefore the number of removed boards is at most
Now we show this bound is actually reachable, because otherwise it's just math cosplay.
Leave boards numbered
Remove every other board. Between two kept boards, there are exactly removed boards. Before the first kept board, there are also removed boards. After the last kept board, there are removed boards, which is less than .
So the longest consecutive removed segment has length at most . The mower cannot escape, and we removed exactly
boards.
Edge cases behave nicely:
For each test case, the answer is:
Time complexity: per test case.
Memory complexity: .
#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 n, w;
cin >> n >> w;
cout << n - n / w << '\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--) {
ll n, w;
cin >> n >> w;
cout << n - n / w << '\n';
}
return 0;
}