#P3090. [USACO13NOV] Empty Stalls G
[USACO13NOV] Empty Stalls G
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, determine the smallest index of a stall that remains unoccupied after all the cows have returned to the barn. Notice that the answer does not depend on the order in which the cows return.
To avoid reading huge amounts of input, the input is specified concisely using lines (), each of the form: X Y A B
One such line specifies preferences for cows in total: cows prefer each of the stalls , where . The values of and lie in the range .
Note: The standard memory limit is 64 MB.
Description
- Line 1: Two space-separated integers and .
- Lines : Each line contains integers , interpreted as above. The total number of cows specified by all lines will be at most . Multiple lines may add cows preferring the same stall.
Output Format
- Line 1: The minimum index of an unoccupied stall.
10 3
3 2 2 4
2 1 0 1
1 1 1 7
5
Hint
There are 10 stalls in the barn, numbered . The second line of input states that 3 cows prefer stall , and 3 cows prefer stall . The third line states that 2 cows prefer stall . Line four specifies that 1 cow prefers stall (so a total of 4 cows prefer this stall).
All stalls will end up occupied except stall 5.
Translated by ChatGPT 5
京公网安备 11011102002149号