Advertisement
informaticage

Xmas Lights OIS

Mar 7th, 2023
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main(void) {
  9.   int N, K;
  10.   cin >> N >> K;
  11.   vector<int> bulbs(N, 0);
  12.   for (int &bulb : bulbs) {
  13.     cin >> bulb;
  14.   }
  15.  
  16.   vector<int> d_map(K, 0);
  17.   int c_min = numeric_limits<int>::max();
  18.  
  19.   int i = 0, j = 0, current_bulbs = 0;
  20.   while (i < N && j < N) {
  21.     if (current_bulbs < K) {
  22.       // Increasing window
  23.       if (d_map.at(bulbs.at(i)) == 0) {
  24.         d_map.at(bulbs.at(i))++;
  25.         current_bulbs++;
  26.       } else {
  27.         d_map.at(bulbs.at(i))++;
  28.       }
  29.       i++;
  30.     } else {
  31.       // Decreasing window
  32.       if (d_map.at(bulbs.at(j)) == 1) {
  33.         d_map.at(bulbs.at(j))--;
  34.         current_bulbs--;
  35.       } else {
  36.         d_map.at(bulbs.at(j))--;
  37.       }
  38.       j++;
  39.     }
  40.  
  41.     if (current_bulbs == K) {
  42.       c_min = min(c_min, i - j);
  43.     }
  44.   }
  45.  
  46.   cout << c_min;
  47.   return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement