Advertisement
Josif_tepe

Untitled

Jul 20th, 2024
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. int main(){
  5.   int n, k;
  6.   cin >> n >> k;
  7.  
  8.   int niza[n];
  9.   for(int i = 0; i < n; i++) {
  10.     cin >> niza[i];
  11.   }
  12.  
  13.   vector<int> cnt(100005, 0);
  14.   int sum = 0;
  15.   int unique = 0;
  16.   for(int i = 0; i < k; i++) {
  17.     sum += niza[i];
  18.     cnt[niza[i]]++;
  19.  
  20.     if(cnt[niza[i]] == 1) {
  21.       unique++;
  22.     }
  23.   }
  24.   cout << unique << " " ;
  25.   int res = 0;
  26.   if(unique == k) {
  27.     res = max(res, sum);
  28.   }
  29.  
  30.   for(int i = k; i < n; i++) {
  31.     sum -= niza[i - k];
  32.     sum += niza[i];
  33.  
  34.     cnt[niza[i - k]]--;
  35.     if(cnt[niza[i - k]] == 0) {
  36.       unique--;
  37.     }
  38.     cnt[niza[i]]++;
  39.  
  40.     if(cnt[niza[i]] == 1) {
  41.       unique++;
  42.     }
  43.     cout << unique << " ";
  44.  
  45.     if(unique == k) {
  46.       res = max(res, sum);
  47.     }
  48.  
  49.   }
  50.   cout << endl;
  51.   cout << res << endl;
  52.    
  53.  return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement