POJ 3320 Jessica ' s Reading problem

Source: Internet
Author: User
Tags cmath

Test instructions: There are n pages, each page has a knowledge point numbered CI, to minimize the number of consecutive pages, including all the knowledge points

Analysis: N<=1e6, can only make O (n) solution, is simply enumerated starting point and end point, the ruler method is suitable for this idea, enumerate a starting point, and then scan to the interval to include all the knowledge points, and then each start to move to the right once, until the scan to the right border and no answer, jump out

The program ran 469ms, should be map too slow, can hash a wave, also too, but can be discretized knowledge point number, up to 1e6, space change time

#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<map>using namespacestd;Const intmaxn=1e6+5;intC[maxn];map<int,int>v;intMain () {intN;  while(~SCANF ("%d",&N))        {v.clear (); intCount=0;  for(intI=0; i<n;i++) {scanf ("%d", c+i); if(v[c[i]]==0) {V[c[i]]=1; Count++; }        }        intans=n+1;intCnt=0, l=0, r=0;        V.clear ();  for(;;) {             while(r<n&&cnt<count) {                if(v[c[r]]==0) cnt++; V[c[r++]]++; }            if(Cnt<count) Break; Ans=min (ans,r-l); if(v[c[l]]==1) cnt--; V[c[l++]]--; } printf ("%d\n", ans); }    return 0;}
View Code

Hash, ran the 250ms, the speed of the increase of half

#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<map>#include<cstring>using namespacestd;Const intmaxn=1e6+5;intC[maxn],v[maxn];inlineintHashintx) {    returnX%699997;}intMain () {intN;  while(~SCANF ("%d",&N)) {memset (V,0,sizeof(v)); intCount=0;  for(intI=0; i<n;i++) {scanf ("%d", c+i); if(V[hash (C[i])]==0) {V[hash (C[i])]=1; Count++; }        }        intans=n+1;intCnt=0, l=0, r=0; memset (V,0,sizeof(v));  for(;;) {             while(r<n&&cnt<count) {                if(V[hash (C[r])]==0) cnt++; V[hash (C[r++])]++; }            if(Cnt<count) Break; Ans=min (ans,r-M); if(V[hash (c[l])]==1) cnt--; V[hash (C[l++])]--; } printf ("%d\n", ans); }    return 0;}
View Code

POJ 3320 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.