# | User | Rating |
---|---|---|

1 | tourist | 3851 |

2 | jiangly | 3634 |

3 | Um_nik | 3539 |

4 | slime | 3498 |

5 | ksun48 | 3493 |

6 | djq_cpp | 3486 |

7 | maroonrk | 3471 |

8 | Radewoosh | 3442 |

9 | Petr | 3426 |

10 | Isonan | 3344 |

# | User | Contrib. |
---|---|---|

1 | -is-this-fft- | 185 |

2 | awoo | 180 |

3 | dario2994 | 171 |

4 | SecondThread | 169 |

5 | maroonrk | 168 |

5 | Um_nik | 168 |

5 | adamant | 168 |

8 | YouKn0wWho | 166 |

8 | errorgorn | 166 |

10 | Monogon | 162 |

Codeforces (c) Copyright 2010-2022 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Sep/26/2022 09:37:25 (i1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

We can start by sorting all plates. This makes it possible for us to see how the orientation will be after finishing the operations. Now, we can see, in the problem's example,

`[1w 2w 3b 4w 5b]`

has 2 partitions for white, 2 partitions for black. As both the original stack and the results are sorted, the original stack is a subsequence of the final sequence. Hence, the answer, as you may have noticed, is $$$2P-c-1$$$ ($$$P$$$ being number of partitions, $$$c$$$ being the amount of unique colors).Now for the tiebreaking part. We want each range of same numbers to have a minimal amount partitions, because more partitions = more operations. Therefore, for each range of same numbers, we match the first partition to the color above, and match the last partition to the color below (if possible). In this way, we do not have to split the first partition and the last partition, and the total partition count is reduced. Do notice that the previous color may not yet be determined, in this case we have to write a DP solution, $$$O(cN+NlogN)$$$ precisely.TL;DR: minimize the amount of partitions on the constraint that the final sequence is sorted, and then the answer is $$$2P-c-1$$$ where $$$P$$$ is the number of partitions and $$$c$$$ is the amount of stacks originally