Question Link: Http://poj.org/problem? Id = 3320
Theme: A book has P pages, and each page has a knowledge point that can be repeated. Ask for at least several consecutive pages to cover all the knowledge points.
Solutions:
The number of unique elements must be counted and the number of repeated elements must be recorded.
It is best to timely report the number of non-duplicates. There is no doubt that hash must be used.
We recommend that you use map, which can be hash or record the number of each key.
Concept of ruler acquisition:
① Extend R without stopping, and drop the knowledge points in the Map until the size of the map meets the requirements.
② Update the result.
② The number of keys (A [L ++]) in the map is-1, which is equivalent to removing this page.
If the number of corresponding keys is <= 0, erase should drop the key to prevent misjudgment of MAP: size.
#include "cstdio"#include "map"using namespace std;int a[1000005];int main(){ //freopen("in.txt","r",stdin); int n,s,l=1,r=1,ans=0x3f3f3f3f; scanf("%d",&n); map<int,int> Hash,x; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); Hash[a[i]]++; } s=Hash.size(); while(true) { while(x.size()<s&&r<=n) x[a[r++]]++; if(x.size()<s) break; ans=min(ans,r-l); x[a[l++]]--; if(x[a[l-1]]<=0) x.erase(a[l-1]); } printf("%d\n",ans);}
13593020 |
Neopenx |
3320 |
Accepted |
1520 K |
422 Ms |
C ++ |
549b |
00:30:01 |
Poj 3320 (ruler acquisition + hash)