This problem is a simple application of the ruler method.
The topic probably means: a person to review a book, each page of this book has a knowledge point AI, each page of knowledge may be the same knowledge points on other pages, ask you how to read the minimum page, so the knowledge point read.
Use the set in the STL to determine how many different points of knowledge there are in NUM, using the map in the STL to represent the mapping of the knowledge point to the number of occurrences.
The same set of knowledge points sum, the start and end points of the page s and T. First, the knowledge point array A is added to the map, until the sum >= num, the update needs to see how many pages, remove the beginning of the page, to determine whether the knowledge point is less, less, then t again self-increment, continue to join, otherwise, then remove the beginning of the page. Keep looping and update the minimum number of pages you need to see.
Here is the AC code:
#include <iostream> #include <cstdio> #include <map> #include <set>using namespace Std;int n;int A[1000005];int min (int x, int y) {return x > y y:x;} void Solve () {set<int>count;for (int i = 0; i < n; i++) //number has several different knowledge points Count.insert (a[i]); int num = Count.size (); Map<int, int>finds; The mapping of knowledge points with occurrences of int s, T, sum;s = t = sum = 0; Start and end pages, initialization of knowledge points int res = N;while (1) {while (T < n && sum < num) if (finds[a[t++]]++ = = 0) //New knowledge point appears sum++ ; Increase the number of knowledge points if (sum < num) //knowledge points less than the total, you can exit the loop, to the end of the Break;res = min (res, t-s); Calculate the number of pages if (--finds[a[s++]] = = 0) //Remove the Start page, the point of elimination of the knowledge points, whether it is 0, is the knowledge points-1sum--;} printf ("%d\n", res);} int main () {scanf ("%d", &n), for (int i = 0; i < n; i++) scanf ("%d", &a[i]); solve (); return 0;}
Pku Acm3320--jessica ' s Reading problem