# 1726 milking sequence (classified discussion, simulation)

Posted by SheepWoolie on Sat, 26 Feb 2022 11:02:20 +0100

1. Problem Description:

Farmer John has n cows numbered 1... N. He milks his cows every day. The social structure of dairy cows is very complex, and its structure has two key characteristics. First of all, there are M cows with distinct status levels. According to the rule that the higher the status, the earlier the milking, the relative milking order of these cows is fixed. In addition, the specific milking sequence of K cows is also fixed. For example, cow 4 must be milked in the second place among all cows. Fortunately, farmer John is always able to milk his cows in an order that meets all these conditions. Unfortunately, cow 1 has been ill recently, so Farmer John wants to milk the cow as soon as possible so that she can go back to the barn and rest. Please help farmer John find the earliest position where cow 1 can appear in the milking sequence.

Input format

The first line contains N, m and K, indicating that Farmer John has N cows, of which m head forms a social class, and K head needs to be in a specific position in the milking sequence. The next line contains M different integers mi. Cows appearing in this line must be milked in the same order as they appear in this line. The following K lines contain two integers ci and pi, indicating that the cow ci must be milked at position pi. Input data guarantee: under these restrictions, Farmer John can establish a satisfactory milking sequence.

Output format

The earliest position in which output cow 1 can appear in the milking sequence.

Data range

2 ≤ N ≤ 100，
1 ≤ M，K < N，
1 ≤ mi，ci，pi ≤ N

Input sample:

6 3 2
4 5 6
5 3
3 1

Output example:

4
Example explanation
In this example, Farmer John has six cows, of which cow 1 is ill. His milking sequence should be cow 4 before cow 5 and cow 5 before cow 6. In addition, Farmer John must be the first to milk cow 3 and the third to milk cow 5. FJ must be the first to milk cow 3. Because cow 4 must be before cow 5, cow 4 must be the second to milk, and then cow 5 is the third. Therefore, cow 1 first appeared in the milking sequence is the fourth.
Source: https://www.acwing.com/problem/content/description/1728/

2. Train of thought analysis:

By analyzing the topic, we can know that the position of k cattle is fixed, and the relative order of M cattle is also fixed. We need to solve the problem. On the premise of meeting the limitation of the topic, make the position of the cattle with number 1 closer to the front. We can find that it corresponds to three situations. Classified discussion can be done: ① the cattle with number 1 already has a fixed position; ② Cattle numbered 1 need to be in the relative order of M cattle; ③ Not in the limit in ① ②; In fact, it is easy to judge the case of ①. We can judge whether there is 1 when entering data, mainly the judgment of ② ③. For case ②, because the cattle numbered 1 are in the relative order limit of M cattle, in order to make the position closer to the front, we need to place these cattle from front to back in the remaining positions; In case ③, in order to make the position of the cow numbered 1 closer to the front, it is necessary to place m cows from the back to the front in the remaining positions, and the first position not placed from the front to the back is the position of the cow numbered 1; Special attention should be paid to the case ② ③. If the cow currently traversed has a fixed position, the current cow does not need to be placed. It can be found that it is easy to analyze, but the code is difficult to write.

3. The code is as follows:

```class Solution:
def process(self):
n, m, k = map(int, input().split())
# p[i] indicates the position of the cow numbered I, q stores the relative order that the cow needs to meet, and st indicates whether the current position has been occupied by the cow
p, st = [-1] * (n + 10), [0] * (n + 10)
q = list(map(int, input().split()))
#
for i in range(k):
a, b = map(int, input().split())
p[a] = b
# Indicates that position b has been occupied
st[b] = 1
# Note that the position of cattle numbered 1 is determined
if a == 1: return b
flag = 0
for i in range(m):
# Explain that the cow numbered 1 is in m
if q[i] == 1:
flag = 1
break
if flag == 1:
# The cowboy m numbered 1 indicates that we need to place these cattle from front to back
# j indicates an attempt to put the currently enumerated cow in the current position j
j = 1
for i in range(m):
# Skip occupied locations
while st[j] == 1: j += 1
# If the current cow to be placed has a fixed position, you don't need to place the current cow update j
if p[q[i]] != -1:
j = p[q[i]]
else:
if q[i] == 1:
# The cow with number 1 indicates that the current j is the position of the cow with number 1
return j
else:
st[j] = 1
j += 1
else:
# Explain that if the cow numbered 1 is not in m, then m cows need to be placed from back to front, which is similar to the second case
j = n
for i in range(m - 1, -1, -1):
while st[j] == 1: j -= 1
# The current number q[i] has a fixed position, so you don't need to place it. Update j
if p[q[i]] != -1:
j = p[q[i]]
else:
st[j] = 1
j -= 1
# The first unoccupied position traversed from front to back is the position of the cow numbered 1
for i in range(1, n + 1):
if st[i] == 0: return i

if __name__ == "__main__":
print(Solution().process())```

Topics: Algorithm