Submission #254799

# Submission time Handle Problem Language Result Execution time Memory
254799 2020-07-30T15:36:07 Z model_code Viruses (BOI20_viruses) Java 11
100 / 100
641 ms 41508 KB
import java.io.*;
import java.util.*;

public class Viruses {

    private static class Val implements Comparable<Val> {

        private final long value;
        private final int i;
        private final int st;
        private final int en;

        public Val(long value, int i, int st, int en) {
            this.value = value;
            this.i = i;
            this.st = st;
            this.en = en;
        }

        @Override
        public int compareTo(Val o) {
            if (this.value < o.value) {
                return -1;
            } else if (this.value > o.value) {
                return 1;
            } else {
                if (this.i < o.i) {
                    return -1;
                } else if (this.i > o.i) {
                    return 1;
                } else {
                    if (this.st < o.st) {
                        return -1;
                    } else if (this.st > o.st) {
                        return 1;
                    } else {
                        if (this.en < o.en) {
                            return -1;
                        } else if (this.en > o.en) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                }
            }
        }
    }

    private static class Mutation {

        private final int st;
        private final int en1;
        private final Integer en2;

        public Mutation(int st, int en1) {
            this.st = st;
            this.en1 = en1;
            this.en2 = null;
        }

        public Mutation(int st, int en1, int en2) {
            this.st = st;
            this.en1 = en1;
            this.en2 = en2;
        }
    }

    private static final int MAXG = 305;
    private static final int MAXL = 55;
    private static final long MYINFLL = 1000000000000000000L;

    private static long[] a = new long[MAXG * MAXL * MAXL + 1];

    private static void set(int i, int st, int en, long val) {
        a[MAXL * MAXL * i + MAXL * st + en] = val;
    }

    private static long get(int i, int st, int en) {
        return a[MAXL * MAXL * i + MAXL * st + en];
    }

    private static TreeSet<Val> q = new TreeSet<>();

    private static void update(int i, int st, int en, long val) {
        long cur = get(i, st, en);
        if (val < cur) {
            q.remove(new Val(cur, i, st, en));
            set(i, st, en, val);
            q.add(new Val(val, i, st, en));
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String str;
        String[] params;

        str = br.readLine();
        params = str.split(" ");
        int G = Integer.parseInt(params[0]);
        int N = Integer.parseInt(params[1]);
        int M = Integer.parseInt(params[2]);
        int origG = G;

        ArrayList<Mutation> mutations = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            str = br.readLine();
            params = str.split(" ");
            int startGene = Integer.parseInt(params[0]);
            int len = Integer.parseInt(params[1]);

            if (len == 1) {
                int endGene = Integer.parseInt(params[2]);
                mutations.add(new Mutation(startGene, endGene));
            } else {
                int cur = startGene;
                int pos = 2;
                while (true) {
                    int nextGene = Integer.parseInt(params[pos]);
                    pos++;
                    len--;
                    if (len == 1) {
                        int finalGene = Integer.parseInt(params[pos]);
                        mutations.add(new Mutation(cur, nextGene, finalGene));
                        break;
                    }
                    mutations.add(new Mutation(cur, nextGene, G));
                    cur = G;
                    G++;
                }
            }
        }

        int[] mutationsByRhsSt = new int[G + 1];
        for (int i = 0; i <= G; i++) {
            mutationsByRhsSt[i] = 0;
        }
        for (int ind = 0; ind < mutations.size(); ind++) {
            Mutation rhs = mutations.get(ind);
            if ((rhs.en2 == null) || (rhs.en2.equals(rhs.en1))) {
                mutationsByRhsSt[rhs.en1]++;
            } else {
                mutationsByRhsSt[rhs.en1]++;
                mutationsByRhsSt[rhs.en2]++;
            }
        }
        for (int i = 1; i <= G; i++) {
            mutationsByRhsSt[i] += mutationsByRhsSt[i - 1];
        }
        int[] mutationsByRhs = new int[mutationsByRhsSt[G] + 1];
        for (int ind = 0; ind < mutations.size(); ind++) {
            Mutation rhs = mutations.get(ind);
            if ((rhs.en2 == null) || (rhs.en2.equals(rhs.en1))) {
                mutationsByRhsSt[rhs.en1]--;
                mutationsByRhs[mutationsByRhsSt[rhs.en1]] = ind;
            } else {
                mutationsByRhsSt[rhs.en1]--;
                mutationsByRhs[mutationsByRhsSt[rhs.en1]] = ind;
                mutationsByRhsSt[rhs.en2]--;
                mutationsByRhs[mutationsByRhsSt[rhs.en2]] = ind;
            }
        }

        Set<String> keywords = new HashSet<>();
        Set<String> prefixesSet = new HashSet<>();
        prefixesSet.add("");

        for (int i = 0; i < M; i++) {
            str = br.readLine();
            params = str.split(" ");
            int len = Integer.parseInt(params[0]);

            String s = "";
            for (int j = 0; j < len; j++) {
                int elem = Integer.parseInt(params[j + 1]);
                s += elem == 1 ? "1" : "0";
            }
            keywords.add(s);
            for (int j = 1; j <= len; j++) {
                prefixesSet.add(s.substring(0, j));
            }
        }

        ArrayList<String> prefixes = new ArrayList<>(prefixesSet.size());
        ArrayList<Boolean> prefixesIsTerminal = new ArrayList<>(prefixesSet.size());
        Map<String, Integer> indexes = new HashMap<>();
        for (String prefix : prefixesSet) {
            indexes.put(prefix, prefixes.size());
            prefixes.add(prefix);

            boolean isTerminal = false;
            for (int i = 0; i < prefix.length(); i++) {
                if (keywords.contains(prefix.substring(i))) {
                    isTerminal = true;
                }
            }

            prefixesIsTerminal.add(isTerminal);
        }

        int S = prefixes.size();

        for (int i = 0; i < G; i++) {
            for (int st = 0; st < S; st++) {
                for (int en = 0; en < S; en++) {
                    set(i, st, en, MYINFLL);
                }
            }
        }

        for (int st = 0; st < S; st++) {
            if (!prefixesIsTerminal.get(st)) {
                for (int i = 0; i < 2; i++) {
                    String s = prefixes.get(st) + (i == 0 ? "0" : "1");
                    while (!prefixesSet.contains(s)) {
                        s = s.substring(1);
                    }
                    int en = indexes.get(s);
                    if (!prefixesIsTerminal.get(en)) {
                        update(i, st, en, 1L);
                    }
                }
            }
        }

        while (true) {
            Val current = q.pollFirst();
            if (current == null) {
                break;
            }
            int i = current.i;
            int st = current.st;
            int en = current.en;
            long cur = current.value;

            for (int pos = mutationsByRhsSt[i]; pos < mutationsByRhsSt[i + 1]; pos++) {
                int ind = mutationsByRhs[pos];
                Mutation mutation = mutations.get(ind);
                if (mutation.en2 == null) {
                    update(mutation.st, st, en, cur);
                } else {
                    if (mutation.en1 == i) {
                        for (int md = 0; md < S; md++) {
                            update(mutation.st, st, md, cur + get(mutation.en2, en, md));
                        }
                    }
                    if (mutation.en2 == i) {
                        for (int md = 0; md < S; md++) {
                            update(mutation.st, md, en, get(mutation.en1, md, st) + cur);
                        }
                    }
                }
            }
        }

        for (int i = 2; i < origG; i++) {
            long res = MYINFLL;
            for (int en = 0; en < S; en++) {
                long cur = get(i, 0, en);
                if (cur < res) {
                    res = cur;
                }
            }
            if (res == MYINFLL) {
                bw.write("YES\n");
            } else {
                bw.write("NO ");
                bw.write(String.valueOf(res));
                bw.write("\n");
            }
        }

        bw.close();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 101 ms 17836 KB Output is correct
2 Correct 102 ms 17748 KB Output is correct
3 Correct 91 ms 17440 KB Output is correct
4 Correct 90 ms 17396 KB Output is correct
5 Correct 87 ms 17532 KB Output is correct
6 Correct 86 ms 17632 KB Output is correct
7 Correct 89 ms 17592 KB Output is correct
8 Correct 87 ms 17484 KB Output is correct
9 Correct 95 ms 17520 KB Output is correct
10 Correct 95 ms 17516 KB Output is correct
11 Correct 98 ms 17828 KB Output is correct
12 Correct 85 ms 17544 KB Output is correct
13 Correct 96 ms 17640 KB Output is correct
14 Correct 112 ms 17556 KB Output is correct
15 Correct 87 ms 17436 KB Output is correct
16 Correct 89 ms 17556 KB Output is correct
17 Correct 88 ms 17668 KB Output is correct
18 Correct 90 ms 17772 KB Output is correct
19 Correct 100 ms 17688 KB Output is correct
20 Correct 92 ms 17724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 17836 KB Output is correct
2 Correct 102 ms 17748 KB Output is correct
3 Correct 164 ms 22252 KB Output is correct
4 Correct 128 ms 21988 KB Output is correct
5 Correct 188 ms 22932 KB Output is correct
6 Correct 206 ms 22920 KB Output is correct
7 Correct 203 ms 23456 KB Output is correct
8 Correct 202 ms 23348 KB Output is correct
9 Correct 173 ms 23032 KB Output is correct
10 Correct 86 ms 17512 KB Output is correct
11 Correct 96 ms 17656 KB Output is correct
12 Correct 88 ms 17780 KB Output is correct
13 Correct 141 ms 22252 KB Output is correct
14 Correct 109 ms 18920 KB Output is correct
15 Correct 127 ms 22028 KB Output is correct
16 Correct 96 ms 17772 KB Output is correct
17 Correct 128 ms 22292 KB Output is correct
18 Correct 117 ms 19204 KB Output is correct
19 Correct 121 ms 22060 KB Output is correct
20 Correct 116 ms 20000 KB Output is correct
21 Correct 120 ms 21992 KB Output is correct
22 Correct 103 ms 17912 KB Output is correct
23 Correct 89 ms 17992 KB Output is correct
24 Correct 81 ms 17564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 22252 KB Output is correct
2 Correct 128 ms 21988 KB Output is correct
3 Correct 188 ms 22932 KB Output is correct
4 Correct 206 ms 22920 KB Output is correct
5 Correct 203 ms 23456 KB Output is correct
6 Correct 202 ms 23348 KB Output is correct
7 Correct 173 ms 23032 KB Output is correct
8 Correct 98 ms 17748 KB Output is correct
9 Correct 97 ms 17652 KB Output is correct
10 Correct 92 ms 17652 KB Output is correct
11 Correct 104 ms 17528 KB Output is correct
12 Correct 91 ms 17652 KB Output is correct
13 Correct 88 ms 17656 KB Output is correct
14 Correct 86 ms 17780 KB Output is correct
15 Correct 83 ms 17912 KB Output is correct
16 Correct 81 ms 17528 KB Output is correct
17 Correct 85 ms 17528 KB Output is correct
18 Correct 118 ms 19084 KB Output is correct
19 Correct 84 ms 17636 KB Output is correct
20 Correct 83 ms 17780 KB Output is correct
21 Correct 83 ms 17528 KB Output is correct
22 Correct 83 ms 17768 KB Output is correct
23 Correct 87 ms 17652 KB Output is correct
24 Correct 97 ms 18024 KB Output is correct
25 Correct 323 ms 27500 KB Output is correct
26 Correct 110 ms 20088 KB Output is correct
27 Correct 641 ms 39240 KB Output is correct
28 Correct 259 ms 24404 KB Output is correct
29 Correct 454 ms 35460 KB Output is correct
30 Correct 432 ms 33880 KB Output is correct
31 Correct 326 ms 27864 KB Output is correct
32 Correct 257 ms 25280 KB Output is correct
33 Correct 221 ms 23916 KB Output is correct
34 Correct 417 ms 33664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 17836 KB Output is correct
2 Correct 102 ms 17748 KB Output is correct
3 Correct 91 ms 17440 KB Output is correct
4 Correct 90 ms 17396 KB Output is correct
5 Correct 87 ms 17532 KB Output is correct
6 Correct 86 ms 17632 KB Output is correct
7 Correct 89 ms 17592 KB Output is correct
8 Correct 87 ms 17484 KB Output is correct
9 Correct 95 ms 17520 KB Output is correct
10 Correct 95 ms 17516 KB Output is correct
11 Correct 98 ms 17828 KB Output is correct
12 Correct 85 ms 17544 KB Output is correct
13 Correct 96 ms 17640 KB Output is correct
14 Correct 112 ms 17556 KB Output is correct
15 Correct 87 ms 17436 KB Output is correct
16 Correct 89 ms 17556 KB Output is correct
17 Correct 88 ms 17668 KB Output is correct
18 Correct 90 ms 17772 KB Output is correct
19 Correct 100 ms 17688 KB Output is correct
20 Correct 92 ms 17724 KB Output is correct
21 Correct 86 ms 17512 KB Output is correct
22 Correct 98 ms 17748 KB Output is correct
23 Correct 97 ms 17652 KB Output is correct
24 Correct 92 ms 17652 KB Output is correct
25 Correct 104 ms 17528 KB Output is correct
26 Correct 91 ms 17652 KB Output is correct
27 Correct 88 ms 17656 KB Output is correct
28 Correct 86 ms 17780 KB Output is correct
29 Correct 83 ms 17912 KB Output is correct
30 Correct 81 ms 17528 KB Output is correct
31 Correct 85 ms 17528 KB Output is correct
32 Correct 118 ms 19084 KB Output is correct
33 Correct 84 ms 17636 KB Output is correct
34 Correct 83 ms 17780 KB Output is correct
35 Correct 83 ms 17528 KB Output is correct
36 Correct 83 ms 17768 KB Output is correct
37 Correct 87 ms 17652 KB Output is correct
38 Correct 97 ms 18024 KB Output is correct
39 Correct 78 ms 17664 KB Output is correct
40 Correct 89 ms 17768 KB Output is correct
41 Correct 93 ms 17772 KB Output is correct
42 Correct 95 ms 17636 KB Output is correct
43 Correct 87 ms 17524 KB Output is correct
44 Correct 93 ms 17840 KB Output is correct
45 Correct 89 ms 17652 KB Output is correct
46 Correct 95 ms 17824 KB Output is correct
47 Correct 103 ms 17796 KB Output is correct
48 Correct 120 ms 19188 KB Output is correct
49 Correct 95 ms 17976 KB Output is correct
50 Correct 89 ms 17588 KB Output is correct
51 Correct 87 ms 17896 KB Output is correct
52 Correct 137 ms 19492 KB Output is correct
53 Correct 88 ms 17528 KB Output is correct
54 Correct 116 ms 18860 KB Output is correct
55 Correct 93 ms 17668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 17836 KB Output is correct
2 Correct 102 ms 17748 KB Output is correct
3 Correct 91 ms 17440 KB Output is correct
4 Correct 90 ms 17396 KB Output is correct
5 Correct 87 ms 17532 KB Output is correct
6 Correct 86 ms 17632 KB Output is correct
7 Correct 89 ms 17592 KB Output is correct
8 Correct 87 ms 17484 KB Output is correct
9 Correct 95 ms 17520 KB Output is correct
10 Correct 95 ms 17516 KB Output is correct
11 Correct 98 ms 17828 KB Output is correct
12 Correct 85 ms 17544 KB Output is correct
13 Correct 96 ms 17640 KB Output is correct
14 Correct 112 ms 17556 KB Output is correct
15 Correct 87 ms 17436 KB Output is correct
16 Correct 89 ms 17556 KB Output is correct
17 Correct 88 ms 17668 KB Output is correct
18 Correct 90 ms 17772 KB Output is correct
19 Correct 100 ms 17688 KB Output is correct
20 Correct 92 ms 17724 KB Output is correct
21 Correct 164 ms 22252 KB Output is correct
22 Correct 128 ms 21988 KB Output is correct
23 Correct 188 ms 22932 KB Output is correct
24 Correct 206 ms 22920 KB Output is correct
25 Correct 203 ms 23456 KB Output is correct
26 Correct 202 ms 23348 KB Output is correct
27 Correct 173 ms 23032 KB Output is correct
28 Correct 86 ms 17512 KB Output is correct
29 Correct 96 ms 17656 KB Output is correct
30 Correct 88 ms 17780 KB Output is correct
31 Correct 141 ms 22252 KB Output is correct
32 Correct 109 ms 18920 KB Output is correct
33 Correct 127 ms 22028 KB Output is correct
34 Correct 96 ms 17772 KB Output is correct
35 Correct 128 ms 22292 KB Output is correct
36 Correct 117 ms 19204 KB Output is correct
37 Correct 121 ms 22060 KB Output is correct
38 Correct 116 ms 20000 KB Output is correct
39 Correct 120 ms 21992 KB Output is correct
40 Correct 103 ms 17912 KB Output is correct
41 Correct 89 ms 17992 KB Output is correct
42 Correct 81 ms 17564 KB Output is correct
43 Correct 98 ms 17748 KB Output is correct
44 Correct 97 ms 17652 KB Output is correct
45 Correct 92 ms 17652 KB Output is correct
46 Correct 104 ms 17528 KB Output is correct
47 Correct 91 ms 17652 KB Output is correct
48 Correct 88 ms 17656 KB Output is correct
49 Correct 86 ms 17780 KB Output is correct
50 Correct 83 ms 17912 KB Output is correct
51 Correct 81 ms 17528 KB Output is correct
52 Correct 85 ms 17528 KB Output is correct
53 Correct 118 ms 19084 KB Output is correct
54 Correct 84 ms 17636 KB Output is correct
55 Correct 83 ms 17780 KB Output is correct
56 Correct 83 ms 17528 KB Output is correct
57 Correct 83 ms 17768 KB Output is correct
58 Correct 87 ms 17652 KB Output is correct
59 Correct 97 ms 18024 KB Output is correct
60 Correct 323 ms 27500 KB Output is correct
61 Correct 110 ms 20088 KB Output is correct
62 Correct 641 ms 39240 KB Output is correct
63 Correct 259 ms 24404 KB Output is correct
64 Correct 454 ms 35460 KB Output is correct
65 Correct 432 ms 33880 KB Output is correct
66 Correct 326 ms 27864 KB Output is correct
67 Correct 257 ms 25280 KB Output is correct
68 Correct 221 ms 23916 KB Output is correct
69 Correct 417 ms 33664 KB Output is correct
70 Correct 78 ms 17664 KB Output is correct
71 Correct 89 ms 17768 KB Output is correct
72 Correct 93 ms 17772 KB Output is correct
73 Correct 95 ms 17636 KB Output is correct
74 Correct 87 ms 17524 KB Output is correct
75 Correct 93 ms 17840 KB Output is correct
76 Correct 89 ms 17652 KB Output is correct
77 Correct 95 ms 17824 KB Output is correct
78 Correct 103 ms 17796 KB Output is correct
79 Correct 120 ms 19188 KB Output is correct
80 Correct 95 ms 17976 KB Output is correct
81 Correct 89 ms 17588 KB Output is correct
82 Correct 87 ms 17896 KB Output is correct
83 Correct 137 ms 19492 KB Output is correct
84 Correct 88 ms 17528 KB Output is correct
85 Correct 116 ms 18860 KB Output is correct
86 Correct 93 ms 17668 KB Output is correct
87 Correct 328 ms 28984 KB Output is correct
88 Correct 85 ms 17780 KB Output is correct
89 Correct 92 ms 17524 KB Output is correct
90 Correct 99 ms 17656 KB Output is correct
91 Correct 109 ms 18164 KB Output is correct
92 Correct 492 ms 35496 KB Output is correct
93 Correct 507 ms 32544 KB Output is correct
94 Correct 147 ms 22184 KB Output is correct
95 Correct 99 ms 18548 KB Output is correct
96 Correct 139 ms 22056 KB Output is correct
97 Correct 281 ms 28460 KB Output is correct
98 Correct 158 ms 22416 KB Output is correct
99 Correct 333 ms 27080 KB Output is correct
100 Correct 160 ms 22628 KB Output is correct
101 Correct 222 ms 24720 KB Output is correct
102 Correct 482 ms 41508 KB Output is correct
103 Correct 230 ms 23704 KB Output is correct
104 Correct 383 ms 29572 KB Output is correct
105 Correct 107 ms 18028 KB Output is correct
106 Correct 349 ms 30988 KB Output is correct
107 Correct 534 ms 34716 KB Output is correct