Address http://poj.org/problem?id=3320
answer
Double pointer in the pointer reaches the range required
If the requirements are less than the right to expand if left to meet the requirements of the reduction in area
The following code correctness adjusted a few times and then enter the card TLE card for a long time did not realize .........
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <map> 3 #include <set> 4 #include <algorithm> 5 #include <assert.h> 6 #include <stdio.h> 7 8 9 using namespace std; 10 11 int n, m; 12 13 const int MAX_N = 1000010; 14 15 int v[MAX_N]; 16 17 set<int> setint; 18 19 int minLen = 999999; 20 21 22 int main() 23 { 24 cin >> n; 25 26 for (int i = 0; i < n; i++) { 27 //cin >> v[i]; 28 scanf("%d",&v[i]); 29 setint.insert(v[i]); 30 } 31 32 int total = setint.size(); 33 34 int l = 0; int r = 0; 35 map<int, int> mapcover; 36 mapcover[v[l]]++; 37 int count = 1; 38 39 while(1) { 40 while (count < total) { 41 r++; 42 if (r >= n) { 43 printf("%d\n", minLen); 44 return 0; 45 } 46 mapcover [V [R & lt]] ++ ; 47 IF (mapcover [V [R & lt]] == . 1 ) { 48 // description of a new type is added 49 COUNT ++ ; 50 } 51 is } 52 is 53 is IF (COUNT < Total) { 54 is BREAK ; 55 } 56 is 57 is minLen = min (minLen, R & lt - L + . 1 ); 58 59 // try to refine the entire range from the left end to reduce 60 mapcover [V [L]] - ; 61 is IF (mapcover [V [L ]] == 0) { 62 // illustrate a reducing species 63 is count-- ; 64 } 65 L ++ ; 66 } 67 68 69 the printf ( " % D \ n- " , minLen); 70 return 0 ; 71 is }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <map> 3 #include <set> 4 #include <algorithm> 5 #include <assert.h> 6 #include <stdio.h> 7 8 9 using namespace std; 10 11 int n, m; 12 13 const int MAX_N = 1000010; 14 15 int v[MAX_N]; 16 17 set<int> setint; 18 19 int minLen = 999999; 20 21 int main() 22 { 23 cin >> n; 24 25 for (int i = 0; i < n; i++) { 26 scanf("%d",&v[i]); 27 setint.insert(v[i]); 28 } 29 30 int total = setint.size(); 31 32 int l = 0; int r = 0; 33 map<int, Int > mapcover; 34 is mapcover [V [L]] ++ ; 35 int COUNT = . 1 ; 36 the while ( . 1 ) { 37 [ IF (COUNT < Total) { 38 is R & lt ++ ; 39 IF (R & lt> = n-) BREAK ; 40 mapcover [V [R & lt]] ++ ; 41 is // adds a new element type element then count + 1'd 42 is IF (mapcover [V [R & lt]] == . 1 ) cOUNT ++ ; 43 is 44 is } 45 the else if(count == total) { 46 minLen = min(minLen, r - l + 1); 47 mapcover[v[l]]--; 48 if (mapcover[v[l]] == 0) { 49 count--; 50 } 51 l++; 52 if (l >= n) break; 53 if (l > r) { 54 r = l; 55 } 56 } 57 else { 58 assert(0); 59 } 60 61 } 62 63 printf("%d\n",minLen); 64 65 66 return 0; 67 }