Submission #1268696

#TimeUsernameProblemLanguageResultExecution timeMemory
1268696rtri Martian DNA (BOI18_dna)C++20
100 / 100
74 ms1864 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k, r;
  cin >> n >> k >> r;

  vector<int> dna(n);
  for (int i = 0; i < n; i++)
    cin >> dna[i];

  vector<int> req(k, -1e9);
  for (int i = 0; i < r; i++) {
    int b, q;
    cin >> b >> q;
    req[b] = q;
  }

  int satisfied_count = 0;
  for (int i = 0; i < k; i++)
    if (req[i] < 0)
      satisfied_count++;

  int shortest = 1e9;
  int right = 0;
  for (int left = 0; left < n; left++) {
    while (right < n && satisfied_count != k) {
      req[dna[right]]--;
      if (req[dna[right]] == 0)
        satisfied_count++;

      right++;
    }

    if (satisfied_count == k && right - left < shortest) {
      shortest = right - left;
    }

    req[dna[left]]++;
    if (req[dna[left]] == 1)
      satisfied_count--;
  }

  if (shortest == 1e9)
    cout << "impossible" << endl;
  else
    cout << shortest << endl;

  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...