fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define el cout << '\n'
  4. #define sz(a) (ll)a.size()
  5. #define all(a) a.begin(),a.end()
  6.  
  7. using namespace std;
  8.  
  9. void solve()
  10. {
  11. int n,m;
  12. cin >> n >> m;
  13. int a[n+5];
  14. for(int i=1;i<=n;++i)
  15. cin >> a[i];
  16. unordered_map<int,int> mp;
  17. int tmp=0;
  18. int l=1;
  19. int ans=INT_MAX;
  20. for(int r=1;r<=n;++r){
  21. if(mp[a[r]]==0)
  22. tmp++;
  23. mp[a[r]]++;
  24. while(tmp==m){
  25. ans=min(ans,r-l+1);
  26. mp[a[l]]--;
  27. if(mp[a[l]]==0)
  28. tmp--;
  29. l++;
  30. }
  31. }
  32. cout << ans;
  33. el;
  34. }
  35.  
  36. int main()
  37. {
  38. ios_base::sync_with_stdio(0);
  39. cin.tie(0),cout.tie(0);
  40. solve();
  41. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
2147483647