87802024-01-30 00:32:30rennKutyavetélkedőjavaAccepted 100/100799ms262116 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])));
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted93ms46920 KiB
2Accepted93ms47124 KiB
subtask215/15
3Accepted89ms48368 KiB
4Accepted90ms49324 KiB
5Accepted86ms49104 KiB
6Accepted92ms50576 KiB
7Accepted293ms94644 KiB
8Accepted314ms98480 KiB
9Accepted324ms100304 KiB
subtask319/19
10Accepted87ms51264 KiB
11Accepted90ms51436 KiB
12Accepted86ms51656 KiB
13Accepted85ms51832 KiB
14Accepted92ms52172 KiB
15Accepted92ms51712 KiB
16Accepted92ms51820 KiB
subtask434/34
17Accepted127ms57468 KiB
18Accepted160ms63196 KiB
19Accepted194ms65872 KiB
20Accepted194ms65852 KiB
21Accepted238ms75076 KiB
22Accepted252ms74608 KiB
subtask532/32
23Accepted603ms143800 KiB
24Accepted615ms152028 KiB
25Accepted708ms167712 KiB
26Accepted639ms165152 KiB
27Accepted680ms169376 KiB
28Accepted799ms210988 KiB
29Accepted759ms262116 KiB
30Accepted791ms261272 KiB
31Accepted660ms192840 KiB
32Accepted635ms149440 KiB
33Accepted727ms213772 KiB
34Accepted676ms210816 KiB