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;
    }
  • 相关阅读:
    Html 播放 mp4格式视频提示 没有发现支持的视频格式和mime类型
    如何防止网站短信验证码被攻击
    JS和C#.NET获取客户端IP
    H5案例分享:移动端touch事件判断滑屏手势的方向
    防止asp.net连续点击按钮重复提交
    JS正则表达式验证手机号和邮箱
    sql server查询数据库的大小和各数据表的大小
    大型分布式网站架构技术总结
    一个高逼格开发者必须理解的大型分布式网站的几点概念
    C# 在程序中控制IIS服务或应用程序池关闭重启
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5651530.html
Copyright © 2011-2022 走看看