Schlemiel the Painter's algorithm