Restaurant Guide
Time limit2sMemory limit1024 MB
Split restaurants ranked by two critics into the most star levels, each holding at least K restaurants, with higher stars ranked above in both rankings.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
Yoon is building a guide app, UNIST Guide, that recommends restaurants near UNIST. Yoon picked restaurants and numbered them from to .
Yoon found rankings from two food critics, Donggyu and Wone, who each ranked all restaurants. Yoon wants to assign ratings based on these two rankings. A rating is an integer star count from to , where is a positive integer. A higher star count means a tastier restaurant.
A rating must not contradict either critic's ranking. That is, if restaurant X has a higher rating than restaurant Y, then X must rank above Y in both critics' rankings. The converse does not need to hold.
Every rating level must also be given to at least restaurants.
Yoon believes the guide becomes more credible when its information is more detailed, so Yoon wants to split the ratings as finely as possible. In other words, Yoon wants the highest star count to be as large as possible. When the ratings satisfy all the conditions, what is the maximum number of rating levels?
Input
The first line contains two integers and , separated by a space, where is the number of restaurants and is the minimum number of restaurants for each rating level. ()
The second line contains the restaurant numbers in the order of Donggyu's ranking, from highest to lowest, separated by spaces.
The third line contains the restaurant numbers in the order of Wone's ranking, from highest to lowest, separated by spaces.
Each restaurant number is an integer from to , and no number repeats within a line.
Output
Print the maximum number of rating levels that Yoon can achieve when the ratings are split as finely as possible.