Poj 3320 (ruler acquisition + hash)

Source: Internet
Author: User

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)

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.