Alternate Solution - Binary Search
We can binary search for the longest possible prefix. A prefix of length is just a prefix of length without a plate; if is it possible to wash all plates of prefix then it must also be possible to wash a prefix of . Now we only need to simulate the stacking and washing. As the official analysis outlines, if plate is on top of plate on the same stack, then must be less than , or else we cannot access the plate beneath it.
We can greedily place each plate on stack if the top plate on stack is larger than our current plate and is the smallest out of all the other soapy stacks. This is because it is always more optimal to satisfy a stricter constraint, rather overriding a broader one if we place the plate on another stack which the plate on top is not the smallest possible.
Implementation
C++
#include <bits/stdc++.h>using namespace std;int main() {ifstream fin("dishes.in");int n;fin >> n;vector<int> order(n);for (int i = 0; i < n; i++) { fin >> order[i]; }
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!