bzoj#P3310. [USACO2013 Nov] Empty Stalls
[USACO2013 Nov] Empty Stalls
Description
Farmer John's new barn consists of a huge circle of stalls, numbered , with stall being adjacent to stall .
At the end of each day, FJ's cows arrive back at the barn one by one, each with a preferred stall they would like to occupy. However, if a cow's preferred stall is already occupied by another cow, she scans forward sequentially from this stall until she finds the first unoccupied stall, which she then claims. If she scans past stall , she continues scanning from stall .
Given the preferred stall of each cow, please determine the smallest index of a stall that remains unoccupied after all the cows have returned to the barn. Notice that the answer to this question does not depend on the order in which the cows return to the barn.
In order to avoid issues with reading huge amounts of input, the input to this problem is specified in a concise format using lines each of the form: .
One of these lines specifies the preferred stall for total cows: X cows prefer each of the stalls , where . Do not forget the standard memory limit of for all problems.
Input
Line 1: Two space-separated integers: .
Lines : Each line contains integers , interpreted as above. The total number of cows specified by all these lines will be at most . Cows can be added to the same stall by several of these lines.
Output
Line : The minimum index of an unoccupied stall.
10 3
3 2 2 4
2 1 0 1
1 1 1 7
5
Sample Explain 1
There are stalls in the barn, numbered . The second line of input states that cows prefer stall , and cows prefer stall . The third line states that cows prefer stall . Line four specifies that cow prefers stall (so a total of cows prefer this stall).
All stalls will end up occupied except stall .
Data scale and Agreement
For data, , , .
Source
Gold