8780 | 2024-01-30 00:32:30 | renn | Kutyavetélkedő | java | Accepted 100/100 | 799ms | 262116 KiB |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import static java.lang.Math.max;
public class main {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int n, k, m, a, b;
String[] be = reader.readLine().split(" ");
n = Integer.parseInt(be[0]);
k = Integer.parseInt(be[1]);
Integer[] feladatok = new Integer[n+2];
Integer[] legjobb = new Integer[n+2];
HashSet<Integer>[] szabalyok = new HashSet[k+1];
be = reader.readLine().split(" ");
for (int i = 0; i < n; i++){
feladatok[i] = Integer.parseInt(be[i])-1;
legjobb[i] = -1;
}
feladatok[n] = feladatok[n+1] = legjobb[n] = legjobb[n+1] = -1;
for (int i = 0; i < k; i++){
szabalyok[i] = new HashSet<>();
}
if (feladatok[0] == k && feladatok[1] == k) {
System.out.println("0");
return;
}
m = Integer.parseInt(reader.readLine());
while (m > 0) {
m--;
be = reader.readLine().split(" ");
a = Integer.parseInt(be[0])-1;
b = Integer.parseInt(be[1])-1;
if (a == k || b == k) continue;
szabalyok[a].add(b);
}
legjobb[0] = feladatok[0] == k ? -1 : 1;
legjobb[1] = feladatok[1] == k ? -1 : 1;
for (int i = 0, j = 0; i < n; i++) {
if (j == 2) break;
if (legjobb[i] == -1) {
j++;
continue;
}
j = 0;
if (szabalyok[feladatok[i]].contains(feladatok[i + 1]))
legjobb[i + 1] = max(legjobb[i + 1], legjobb[i] + 1);
if (szabalyok[feladatok[i]].contains(feladatok[i + 2]))
legjobb[i + 2] = max(legjobb[i + 2], legjobb[i] + 1);
}
System.out.println(max(0, max(legjobb[n - 1], legjobb[n - 2])));
}
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 93ms | 46920 KiB | ||||
2 | Accepted | 93ms | 47124 KiB | ||||
subtask2 | 15/15 | ||||||
3 | Accepted | 89ms | 48368 KiB | ||||
4 | Accepted | 90ms | 49324 KiB | ||||
5 | Accepted | 86ms | 49104 KiB | ||||
6 | Accepted | 92ms | 50576 KiB | ||||
7 | Accepted | 293ms | 94644 KiB | ||||
8 | Accepted | 314ms | 98480 KiB | ||||
9 | Accepted | 324ms | 100304 KiB | ||||
subtask3 | 19/19 | ||||||
10 | Accepted | 87ms | 51264 KiB | ||||
11 | Accepted | 90ms | 51436 KiB | ||||
12 | Accepted | 86ms | 51656 KiB | ||||
13 | Accepted | 85ms | 51832 KiB | ||||
14 | Accepted | 92ms | 52172 KiB | ||||
15 | Accepted | 92ms | 51712 KiB | ||||
16 | Accepted | 92ms | 51820 KiB | ||||
subtask4 | 34/34 | ||||||
17 | Accepted | 127ms | 57468 KiB | ||||
18 | Accepted | 160ms | 63196 KiB | ||||
19 | Accepted | 194ms | 65872 KiB | ||||
20 | Accepted | 194ms | 65852 KiB | ||||
21 | Accepted | 238ms | 75076 KiB | ||||
22 | Accepted | 252ms | 74608 KiB | ||||
subtask5 | 32/32 | ||||||
23 | Accepted | 603ms | 143800 KiB | ||||
24 | Accepted | 615ms | 152028 KiB | ||||
25 | Accepted | 708ms | 167712 KiB | ||||
26 | Accepted | 639ms | 165152 KiB | ||||
27 | Accepted | 680ms | 169376 KiB | ||||
28 | Accepted | 799ms | 210988 KiB | ||||
29 | Accepted | 759ms | 262116 KiB | ||||
30 | Accepted | 791ms | 261272 KiB | ||||
31 | Accepted | 660ms | 192840 KiB | ||||
32 | Accepted | 635ms | 149440 KiB | ||||
33 | Accepted | 727ms | 213772 KiB | ||||
34 | Accepted | 676ms | 210816 KiB |