Submission #1353639

#TimeUsernameProblemLanguageResultExecution timeMemory
1353639cnam9 Martian DNA (BOI18_dna)C++20
100 / 100
14 ms1988 KiB
#include <iostream>
#include <vector>

using namespace std;


signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // freopen("input.txt", "r", stdin);

    int length, alphabet, requirements;
    cin >> length >> alphabet >> requirements;

    vector<int> dna(length);
    for (int &nucleotide : dna) {
        cin >> nucleotide;
    }

    int high = 0;

    vector<int> requiers(alphabet);
    while (requirements--) {
        int nucleobase, require;
        cin >> nucleobase >> require;

        requiers[nucleobase] += require;

        while (requiers[nucleobase] > 0) {
            if (high == length) {
                cout << "impossible";
                return 0;
            }
            requiers[dna[high++]]--;
        }
    }

    int res = length;

    for (int low = 0; low < length; low++) {
        res = min(res, high - low);

        int nucleobase = dna[low];
        requiers[nucleobase]++;

        while (requiers[nucleobase] > 0) {
            if (high == length) {
                cout << res;
                return 0;
            }
            requiers[dna[high++]]--;
        }
    }

    cout << res;
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...