PKU Acm3320--jessica ' s Reading problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.