/
MilkingOrder.java
94 lines (94 loc) · 2.87 KB
/
MilkingOrder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.*;
import java.io.*;
public class MilkingOrder {
static ArrayList<Integer> reverseSpecActPos = new ArrayList<>();
static int[] hie;
static HashMap<Integer, Integer> specIndCows;
static int n;
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new BufferedReader(new FileReader("milkorder.in")));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("milkorder.out")));
n = scan.nextInt();
int m = scan.nextInt();
int k = scan.nextInt();
int[] availSpots = new int[n];
hie = new int[m];
specIndCows = new HashMap<>();
int cur = n-1;
for (int i = 0; i < m; i++) {
hie[i] = scan.nextInt();
}
for (int i = 0; i < k; i++) {
int cow = scan.nextInt();
int ind = scan.nextInt();
specIndCows.put(cow, ind-1);
}
for (int cow : specIndCows.keySet()) {
availSpots[specIndCows.get(cow)] = cow;
}
int finVal = findInd1() + 1;
if (finVal == 0) {
for (int i = m - 1; i >= 0; i--) {
int cow = hie[i];
if (specIndCows.containsKey(cow)) {
cur = specIndCows.get(cow);
cur--;
}
else {
while (cur >= 0 && availSpots[cur] != 0) {
cur--;
}
availSpots[cur] = cow;
cur--;
}
}
for (int i = 0; i < n; i++) {
if (availSpots[i] == 0) {
finVal = i + 1;
break;
}
}
}
pw.println(finVal);
pw.close();
scan.close();
}
public static int findInd1() {
boolean fin = false;
for (int cow : specIndCows.keySet()) {
if (cow == 1) {
return specIndCows.get(1);
}
}
for (int i = 0; i < hie.length; i++) {
if (hie[i] == 1) {
fin = true;
}
}
if (!fin) {
return -1;
}
int cur = 0;
int[] availSpots = new int[n];
for (int cow : specIndCows.keySet()) {
availSpots[specIndCows.get(cow)] = cow;
}
for (int cow : hie) {
if (specIndCows.containsKey(cow)) {
cur = specIndCows.get(cow);
cur++;
}
else {
while (cur < n && availSpots[cur] != 0) {
cur++;
}
availSpots[cur] = cow;
if (cow == 1) {
return cur;
}
cur++;
}
}
return -1;
}
}