zoukankan      html  css  js  c++  java
  • POJ 3320 (尺取法+Hash)

    题目链接http://poj.org/problem?id=3320

    题目大意:一本书有P页,每页有个知识点,知识点可以重复。问至少连续读几页,使得覆盖全部知识点。

    解题思路

    知识点是有重复的,因此需要统计不重复元素个数,而且需要记录重复个数。

    最好能及时O(1)反馈不重复的个数。那么毫无疑问,得使用Hash。

    推荐使用map,既能Hash,也能记录对于每个key的个数。

     

    尺取的思路:

    ①不停扩展R,并把扫过知识点丢到map里,直到map的size符合要求。

    ②更新结果。

    ②L++,map里的对应key(a[l++])的个数-1,相当于移出这页。

    如果对应的key的个数<=0,则应该erase掉这个key,防止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
    ",ans);
    }
    13593020 neopenx 3320 Accepted 1520K 422MS C++ 549B 2014-11-03 00:30:01
  • 相关阅读:
    优化webstorm打开项目速度
    组件 -- Button
    组件 --BreadCrumb--面包屑
    组件 -- Badge
    组件 -- Alert
    表格-table 样式
    image 样式设置
    文本和字体样式设置
    bootstrap-网格系统
    c#方法
  • 原文地址:https://www.cnblogs.com/neopenx/p/4070332.html
Copyright © 2011-2022 走看看