|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +typedef long long ll; |
| 5 | +typedef pair<int, int> pii; |
| 6 | + |
| 7 | +#define rep(i, a, b) for (int i = a; i < b; i++) |
| 8 | +#define in1(a) cin >> a; |
| 9 | +#define in2(a, b) cin >> a >> b; |
| 10 | +#define in3(a, b, c) cin >> a >> b >> c; |
| 11 | +#define in4(a, b, c, d) cin >> a >> b >> c >> d; |
| 12 | +#define out1(a) cout << a << endl; |
| 13 | +#define out2(a, b) cout << a << " " << b << endl; |
| 14 | +#define out3(a, b, c) cout << a << " " << b << " " << c << endl; |
| 15 | +#define out4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl; |
| 16 | + |
| 17 | +int n, k, arr[10001], ans=20000; |
| 18 | +map<int, int> p, pp; |
| 19 | + |
| 20 | +int main() { |
| 21 | + ios::sync_with_stdio(false); cin.tie(NULL); |
| 22 | + |
| 23 | + cin>>n>>k; |
| 24 | + for(int i=0;i<n;++i) { |
| 25 | + cin>>arr[i]; |
| 26 | + ++p[arr[i]]; |
| 27 | + } |
| 28 | + |
| 29 | + int s=0,e=0; |
| 30 | + |
| 31 | + while(e < n && s <= e){ |
| 32 | + if(p.size()==k && pp.size()==k) ans = min(ans, e-s); |
| 33 | + |
| 34 | + if(pp.size()==k) { |
| 35 | + ++p[arr[s]]; |
| 36 | + --pp[arr[s]]; |
| 37 | + if(!pp[arr[s]]) pp.erase(arr[s]); |
| 38 | + s++; |
| 39 | + } else { |
| 40 | + ++pp[arr[e]]; |
| 41 | + --p[arr[e]]; |
| 42 | + if(!p[arr[e]]) p.erase(arr[e]); |
| 43 | + e++; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if(ans == 20000) ans = 0; |
| 48 | + cout<<ans<<"\n"; |
| 49 | + return 0; |
| 50 | +} |
0 commit comments