zoukankan      html  css  js  c++  java
  • 尺取法例题

    尺取法:通过左右移动两个端点,遍历一个数列,从而求解一个.....的最短区间的算法,就是尺取法。(像毛毛虫一样  头部移动一段 尾部移动一段)  复杂度O(n)

    题目特征:1.答案满足某个条件的最短连续序列(或区间)

    2.任意两个合法区域(满足题目条件的区间)[a,b],[c,d]。当a>c时,必有d>=b

    满足以上两个条件,首先考虑使用尺取法 

    例1 , POJ3061:Subsequence

    Description

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

    Input

    The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

    Output

    For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

    Sample Input

    2
    10 15
    5 1 3 5 10 7 4 9 2 8
    5 11
    1 2 3 4 5

    Sample Output

    2
    3

    给长度为n的数组和一个整数m,求总和不小于m的连续子序列的最小长度

    n = 10,m = 15

    5 1 3 5 10 7 4 9 2 8

    那么我们先用sum存当前这个子序列的和,从左边第一个数来存,直到这个子序列的和大于等于m为止,再记录下当前长度。

    其实相当于当不满足条件就入队,然后得到队列长度,再将队首元素出队,再进行下一次的

    入队,直到满足条件再次出队,并且将这一次的长度与历史最短长度进行取舍,最后扫到最

    后的元素却无法再满足入队条件的时候就结束,此时用O(n)的时间就可以得到答案。

    5 1 3 5 10 7 4 9 2 8

    5 1 3 5 10 7 4 9 2 8

    5 1 3 5 10 7 4 9 2 8

    5 1 3 5 10 7 4 9 2 8

    5 1 3 5 10 7 4 9 2 8

    5 1 3 5 10 7 4 9 2 8

    5 1 3 5 10 7 4 9 2 8

    5 1 3 5 10 7 4 9 2 8

    总结:1.区间和<S,右端右移

       2. 区间和>S,尝试更新答案,左端右移

    #include<iostream>
    #include<cstdio>
    #include <queue>
    using namespace std;
    int a[100005];
    int main(){
        int T;
        scanf("%d",&T);
        int n,m;
        while(T--)
        {
            scanf("%d%d",&n,&m);
            int ans = n+1;
    
            for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
            }
            int now = 0;
            queue<int> q;
            int flag = 0;
            for(int i=0;i<n;i++)
            {
                q.push(a[i]);
                now+=a[i];
                if(now>m)
                {
                    while(now>=m)
                    {
                        flag = 1;
                        int t = q.size();
                        ans = min(ans,t);
                        now-=q.front();
                        q.pop();
                    }
                }
            }
            if(!flag)
                printf("0
    ");
            else
                printf("%d
    ",ans);
        }
    }

    未完。。

  • 相关阅读:
    24.最优布线问题(kruskal算法)
    24.最优布线问题(kruskal算法)
    Algs4-1.4.11为StaticSETofInts添加一个实列方法howMany()
    Algs4-1.4.9预测程序运行时间
    Algs4-1.4.10二分查找找出元素所在的最小索引
    Algs4-1.4.7统计算术运算与比较次数
    Algs4-1.4.8计算输入文件中相等的整数对的数量
    Algs4-1.4.6给出以下代码段的运行时间的增长数量级
    Algs4- 1.4.4参照表1.4.4为TwoSum建立一和类似的表格
    Algs4-1.4.2修改ThreeSum防止两个int值相加可能溢出
  • 原文地址:https://www.cnblogs.com/hao-tian/p/10163726.html
Copyright © 2011-2022 走看看