POJ_2100_Graveyard_Design_(尺取法)
描述
http://poj.org/problem?id=2100
求连续平方和=n的序列个数,并输出序列.
Time Limit: 10000MS | Memory Limit: 64000K | |
Total Submissions: 5987 | Accepted: 1416 | |
Case Time Limit: 2000MS |
Description
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
Output
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
分析
直接尺取.
注意:
1.如果要用开根计算的话要写成 " ll ub=(ll)sqrt(n*1.0); "写成 " ll ub=sqrt(n); "会CE.
所以干脆写成 " r*r<=n "
#include<cstdio>
#include<queue>
#define ll long long
using std :: queue; struct node
{
ll len,fst;
node() {}
node(ll a,ll b) : len(a),fst(b) {}
};
queue <node> q;
ll n; inline ll val(ll x) { return x*x; } int main()
{
#ifndef ONLINE_JUDGE
freopen("grave.in","r",stdin);
freopen("grave.out","w",stdout);
#endif
scanf("%lld",&n);
ll l=,r=,k=,len=;
ll sum=;
while(val(r)<=n)
{
if(sum<n)
{
sum+=val(++r);
len++;
}
else if(sum>n)
{
sum-=val(l++);
len--;
}
else
{
q.push(node(len,l));
k++;
sum-=val(l++);
len--;
}
}
printf("%lld",k);
while(!q.empty())
{
node t=q.front(); q.pop();
ll len=t.len,fst=t.fst;
printf("\n%lld ",len);
for(ll i=;i<len;i++) printf("%lld ",fst+i);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
POJ_2100_Graveyard_Design_(尺取法)的更多相关文章
- 5806 NanoApe Loves Sequence Ⅱ(尺取法)
传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K ...
- POJ3061 尺取法
题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- CF 701C They Are Everywhere(尺取法)
题目链接: 传送门 They Are Everywhere time limit per test:2 second memory limit per test:256 megabytes D ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- HDU 5358 尺取法+枚举
题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...
随机推荐
- 使用solr搭建你的全文检索
Solr 是一个可供企业使用的.基于 Lucene 的开箱即用的搜索服务器.对Lucene不熟?那么建议先看看下面两篇文档: 实战Lucene,第 1 部分: 初识 Lucene:http://www ...
- 基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置
第一章. 背景简介 对于大多数企业应用,都希望能做到7*24小时不间断运行.要保持如此高的可用性并非易事,比较常见的做法是将系统部署到多台机器上,每台机器都对外提供同样的功能,这就是集群.系统变为集群 ...
- Deep Learning 学习随记(三)续 Softmax regression练习
上一篇讲的Softmax regression,当时时间不够,没把练习做完.这几天学车有点累,又特别想动动手自己写写matlab代码 所以等到了现在,这篇文章就当做上一篇的续吧. 回顾: 上一篇最后给 ...
- Google Test Frame 简单使用例子
1 序言——为什么折腾Google Test 被逼无奈的. 最近研究google开源的基于列存储的数据库查询引擎supersonic源码.初略的浏览了一遍代码,竟然没有main函数,顿时惊讶的目瞪口呆 ...
- bootstrap实现手风琴功能(树形列表)
首先把架包拷进项目,然后在页面中引进css,js <script src="js/jquery/jquery-2.1.1.min.js"></script> ...
- IOS 学习笔记 2015-03-24 OC-API-网络访问-案例一
// // WPSuggest.h // OC-API-网络访问 // // Created by wangtouwang on 15/3/24. // Copyright (c) 2015年 wan ...
- Headfirst设计模式的C++实现——命令模式(Command)
先看如果不用命令模式的实现: light.h #ifndef _LIGHT_H_ #define _LIGHT_H #include <iostream> class LIGHT { pu ...
- php 魔术方法
PHP5.0后,php面向对象提成更多方法,使得php更加的强大!! 一些在PHP叫魔术方法的函数,在这里介绍一下:其实在一般的应用中,我们都需要用到他们!! 1.__construct() 当实例化 ...
- MINA的session.close
现象:客户端session.close之后,并没有提出,客户端程序一直hold在那里: 解决:调用了session.getService().dispose(false)方法后,客户端程序完成了退出 ...
- maven的安装,maven库配置和Eclipse插件的安装
maven的安装,maven库配置和Eclipse插件的安装 1.下载并解压maven 2.配置环境变量 3.配置maven配置文件 1.下载链接 Downloading Apache Maven 2 ...