zoukankan      html  css  js  c++  java
  • 尺取法的学习

    尺取法:顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后根据实际情况不断地推进区间左右端点以得出答案。尺取法比直接暴力枚举区间效率高很多,尤其是数据量大的时候,所以说尺取法是一种高效的枚举区间的方法,是一种技巧,一般用于求取有一定限制的区间个数或最短的区间等等。当然任何技巧都存在其不足的地方,有些情况下尺取法不可行,无法得出正确答案,所以要先判断是否可以使用尺取法再进行计算。

     

    使用尺取法时应清楚以下四点:

    1、  什么情况下能使用尺取法?  2、何时推进区间的端点? 3、如何推进区间的端点? 4、何时结束区间的枚举?

     

        尺取法通常适用于选取区间有一定规律,或者说所选取的区间有一定的变化趋势的情况,通俗地说,在对所选取区间进行判断之后,我们可以明确如何进一步有方向地推进区间端点以求解满足条件的区间,如果已经判断了目前所选取的区间,但却无法确定所要求解的区间如何进一步得到根据其端点得到,那么尺取法便是不可行的。首先,明确题目所需要求解的量之后,区间左右端点一般从最整个数组的起点开始,之后判断区间是否符合条件在根据实际情况变化区间的端点求解答案。

     

        现在我们先举个栗子并分析一下尺取法的过程叭~

    下面用一道例题来讲解:

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

    Graveyard Design
    Time Limit: 10000MS   Memory Limit: 64000K
    Total Submissions: 9572   Accepted: 2426
    Case Time Limit: 2000MS

    Description

    King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves. 
    After a consultation with his astrologer, King George decided that the lengths of section sides must be a sequence of successive positive integer numbers. A section with side length s contains s2 graves. George has estimated the total number of graves that will be located on the graveyard and now wants to know all possible graveyard designs satisfying the condition. You were asked to find them.

    Input

    Input file contains n --- the number of graves to be located in the graveyard (1 <= n <= 1014 ).

    Output

    On the first line of the output file print k --- the number of possible graveyard designs. Next k lines must contain the descriptions of the graveyards. Each line must start with l --- the number of sections in the corresponding graveyard, followed by l integers --- the lengths of section sides (successive positive integer numbers). Output line's in descending order of l.

    Sample Input

    2030

    Sample Output

    2
    4 21 22 23 24
    3 25 26 27

    Source

    Northeastern Europe 2004, Northern Subregion
    题目大意:输入一个数N  判断有哪些连续的数平方相加得到N  输出要求如例子
    看代码:
    #include<iostream>
    #include<stdio.h>
    #include<math.h>
    #include<algorithm>
    #include<vector>
    using namespace std;
    typedef long long ll;
    const ll maxn=1e5+5;
    double N;
    ll Left[maxn],Right[maxn];
    int main()
    {
        cin>>N;
        ll n=sqrt(N);
        ll sum=0;
        ll l=1,r=0;//选取最初的区间端点
        ll cnt=0;
        while(true)
        {
            while(sum<N)//当小于N时  一直移动右边界
            {
                r++;
                sum+=r*r;//同时sum+上
    
            }
            if(r>n) break;//走到最右边了跳出循环
            if(sum==N)//这个处理挺好的  记录一下左右边界 方便最后输出结果
            {
                Left[cnt]=l;
                Right[cnt]=r;
                cnt++;
            }
            sum-=l*l;
            l++;//移动区间左边界
        }
        printf("%lld
    ",cnt);
        for(ll i=0;i<cnt;i++)
        {
            printf("%lld",Right[i]-Left[i]+1);
            for(int j=Left[i];j<=Right[i];j++) printf(" %lld",j);
            printf("
    ");
        }
        return 0;
    }
    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    HTML5存储
    HTML5 地理位置定位(Geolocation)原理及应用
    HTML5多媒体组件的使用
    HTML5拖拽
    HTML5一些总结
    js高级程序设计--数据类型
    JS高级程序设计基本概念
    浏览器内核、渲染引擎、js引擎
    JS高级程序设计笔记之script标签
    CenOS7下安装 nginx
  • 原文地址:https://www.cnblogs.com/caijiaming/p/10392031.html
Copyright © 2011-2022 走看看