zoukankan      html  css  js  c++  java
  • ACM模板——尺取法(滑动区间)

    int S;//S为题目所要求的最小和 
    int a[maxn];//a为题目所给数组 
    int solve(int n)
    {
        int res = n+1;//res为最短长度 
        int s = 0,t = 0,sum = 0;//sum为 目前区间加和 
        while(1)
        {
            while(t<n && sum < S)//扩张区间 
                sum += a[t++]; 
            if(sum < S) break;
            res = min(res,t-s);
            sum -= a[s++];//缩短区间 
        }
        if(res > n)//解不存在 
            return 0; 
        return res; 
    }
    区间和不小于S
    int a[maxn];//a为题目所给数组 
    int solve(int n)
    {
        set<int> all;//计算知识点总数 
        _for(i,0,n)
            all.insert(a[i]);
        n = all.size();
        
        int s = 0, t = 0, num = 0;//num为区间内知识点总数 
        map<int,int> m;
        int res = n;
        while(1)
        {
            while(t<n && num<n)
                if(m[a[t++]]++ == 0)
                    num ++;//出现新知识点
            
            if(num<n) break;
            res = min(res,t-s);
            if(--m[a[s++]] == 0)
                num --;//已经有某个知识点不在区间内了 
        }
        return res; 
    } 
    包含所有知识点的最小区间
  • 相关阅读:
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    Python成长笔记
    解决Jenkins生成测试报告的问题
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10633243.html
Copyright © 2011-2022 走看看