• POJ_2100_Graveyard_Design_(尺取法)


    描述


    http://poj.org/problem?id=2100

    求连续平方和=n的序列个数,并输出序列.

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

    分析


    直接尺取.

    注意:

    1.如果要用开根计算的话要写成 " ll ub=(ll)sqrt(n*1.0); "写成 " ll ub=sqrt(n); "会CE.

    所以干脆写成 " r*r<=n "

     1 #include<cstdio>
     2 #include<queue>
     3 #define ll long long
     4 using std :: queue;
     5 
     6 struct node 
     7 {
     8     ll len,fst;
     9     node() {}
    10     node(ll a,ll b) : len(a),fst(b) {}
    11 };
    12 queue <node> q;
    13 ll n;
    14 
    15 inline ll val(ll x) { return x*x; }    
    16 
    17 int main()
    18 {
    19 #ifndef ONLINE_JUDGE
    20     freopen("grave.in","r",stdin);
    21     freopen("grave.out","w",stdout);
    22 #endif
    23     scanf("%lld",&n);
    24     ll l=1,r=0,k=0,len=0;
    25     ll sum=0;
    26     while(val(r)<=n)
    27     {
    28         if(sum<n)
    29         {
    30             sum+=val(++r);
    31             len++;
    32         }
    33         else if(sum>n)
    34         {
    35             sum-=val(l++);
    36             len--;
    37         }
    38         else
    39         {
    40             q.push(node(len,l));
    41             k++;
    42             sum-=val(l++);
    43             len--;
    44         }
    45     }
    46     printf("%lld",k);
    47     while(!q.empty())
    48     {
    49         node t=q.front(); q.pop();
    50         ll len=t.len,fst=t.fst;
    51         printf("
    %lld ",len);
    52         for(ll i=0;i<len;i++) printf("%lld ",fst+i);
    53     }
    54 #ifndef ONLINE_JUDGE
    55     fclose(stdin);
    56     fclose(stdout);
    57 #endif
    58     return 0;
    59 }
    View Code

     

  • 相关阅读:
    RabbitMQ 3.8.5版本 windows安装和web管理界面登陆
    RabbitMQ 3.8.5版本 4 种Exchange交换机类型选择,关系RabbitMQ架构选型核心
    maven的settings.xml里设置aliyun镜像以及jdk1.8版本编译
    centos8赋予当前用户rpm文件所有权,并安装rpm文件
    Java8里的HashMap对象lambda遍历方法
    springboot项目application.properties配置文件里自定义日志输出
    kafka2.5.0硬件集群架构图、Topic主题与Partitions分区架构图
    kafka2.5.0分区再均衡监听器java例子
    kafka2.5.0自定义分区器
    kafka2.5.0自定义数据序列化类
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5428034.html
Copyright © 2020-2023  润新知