容易联想到尺取法,因为假设从第s页开始阅读至少需要读到t页才能覆盖所有知识点的话,那么如果从s+1页开始阅读,至少要读到t'>=t的位置。于是可以考虑用map维护一下。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <map> 5 using namespace std; 6 7 const int INF = 1111111; 8 const int N = 1000000; 9 int a[N]; 10 map<int, int> mp; 11 12 int main () 13 { 14 int n; 15 while ( scanf("%d", &n) != EOF ) 16 { 17 mp.clear(); 18 for ( int i = 0; i < n; i++ ) 19 { 20 scanf("%d", a + i); 21 mp[a[i]]++; 22 } 23 int sz = mp.size(), j = 0, ans = INF; 24 mp.clear(); 25 for ( int i = 0; i < n; i++ ) 26 { 27 while ( mp.size() < sz && j < n ) 28 { 29 mp[a[j]]++; 30 j++; 31 } 32 if ( mp.size() < sz ) break; 33 ans = min( ans, j - i ); 34 if ( mp[a[i]] == 1 ) 35 { 36 mp.erase(a[i]); 37 } 38 else 39 { 40 mp[a[i]]--; 41 } 42 } 43 printf("%d ", ans); 44 } 45 return 0; 46 }