zoukankan      html  css  js  c++  java
  • poj 2100(尺取法)

    Graveyard Design
    Time Limit: 10000MS   Memory Limit: 64000K
    Total Submissions: 6107   Accepted: 1444
    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


    题意:给你一个数,询问有多少种连续自然数的平方和等于这个数,输出所有可能
    题解:尺取法遍历所有符合条件的区间,满足的话记录左边界以及右边界,计数器+1。
    尺取法过程:

      整个过程分为4布:

        1.初始化左右端点

        2.不断扩大右端点,直到满足条件

        3.如果第二步中无法满足条件,则终止,否则更新结果

        4.将左端点扩大1,然后回到第二步

     

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <stdlib.h>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    LL n;
    struct Node{
        LL l,r;
    }res[1000007];
    int main()
    {
        while(scanf("%lld",&n)!=EOF){
    
            LL l=1,r=1;
            LL len = (int)sqrt(n*1.0)+1;
            LL sum = 0;
            int cnt=0;
            while(l<=len){
                while(r<=len&&sum<n){
                    sum+=r*r;
                    r++;
                }
                if(sum<n) break;
                if(sum==n){
                    cnt++;
                    res[cnt].l = l;
                    res[cnt].r = r;
                }
                sum-=l*l;
                l++;
            }
            printf("%d
    ",cnt);
            for(int i=1;i<=cnt;i++){
                printf("%d ",res[i].r-res[i].l);
                for(int j=res[i].l;j<res[i].r-1;j++){
                    printf("%d ",j);
                }
                printf("%d
    ",res[i].r-1);
            }
        }
        return 0;
    }
  • 相关阅读:
    50个必备的实用jQuery代码段
    js前台改变服务器控件的disable的属性,后台获取不到值
    什么是线程安全?
    解决用JS修改服务器端控件值后在后台无法获取修改后值的问题
    C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
    ASP.NET MVC生命周期介绍
    求一段CSS样式代码;要求是Table的标签样式,实现Table标签奇数行显示一个颜色;偶数行显示另外一种颜色
    JavaScript中Date.parse 函数用法
    sql 获取 一列的值显示一行
    超强 css 实现 table 隔行 ,隔列 换色
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5651530.html
Copyright © 2011-2022 走看看