Josif_tepe

Untitled

Dec 11th, 2025
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. const int INF = 2e9;
  6. int main() {
  7.     ios_base::sync_with_stdio(false);
  8.    
  9.     int n, k, r;
  10.     cin >> n >> k >> r;
  11.    
  12.     vector<int> v(n);
  13.     for(int i = 0; i < n; i++) {
  14.         cin >> v[i];
  15.     }
  16.    
  17.     vector<int> req(k + 1, 0);
  18.     for(int i = 0; i < r; i++) {
  19.         int A, B;
  20.         cin >> A >> B;
  21.         req[A] = B;
  22.     }
  23.    
  24.     int j = 0;
  25.     int res = INF;
  26.    
  27.     for(int i = 0; i < n; i++) {
  28.         while(r > 0 and j < n) {
  29.             req[v[j]]--;
  30.             if(req[v[j]] == 0) {
  31.                 r--;
  32.             }
  33.             j++;
  34.         }
  35.        
  36.         if(r == 0) {
  37.             res = min(res, j - i);
  38.         }
  39.        
  40.         req[v[i]]++;
  41.         if(req[v[i]] > 0) {
  42.             r++;
  43.         }
  44.        
  45.     }
  46.    
  47.     if(res == INF) {
  48.         cout << "impossible" << endl;
  49.     }
  50.     else {
  51.         cout << res << endl;
  52.     }
  53.    
  54.     return 0;
  55. }
  56. /*
  57.  5 2 2
  58.  0 1 1 0 1
  59.  0 1
  60.  1 1
  61.  
  62.  13 4 3
  63.  1 1 3 2 0 1 2 0 0 0 0 3 1
  64.  0 2
  65.  2 1
  66. 1 2
  67.  
  68.  5 3 1
  69.  1 2 0 1 2
  70.  0 2
  71.  */
  72.  
Advertisement
Add Comment
Please, Sign In to add comment