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) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...
随机推荐
- 转载---SQL Server XML基础学习之<5>--XQuery(query)
本章写一些SQL Server XML的一些XQuery基础语法,主要讲的query查询语法 T-SQL 支持用于查询 XML 数据类型的 XQuery 语言的子集. XQuery 基于现有的 XPa ...
- (转)Cookies使用
实际上,在web开发中,cookie仅仅是一个文本文件,当用户访问站点时,它就被存储在用户使用的计算机上,其中,保存了一些信息,当用户日后再次访问这个站点时,web可以将这些信息提取出来. 尽管现在听 ...
- window redis 安装配置
1 下载 https://github.com/MSOpenTech/redis/releases 当前最新版本为 redis-2.8.21 下载的为zip包,下载连接为:https://gith ...
- 动态加载js、css 代码
一.原生js: /** * 加载js和css文件 * @param jsonData.path 前缀路径 * @param jsonData.url 需要加载的js路径或css路径 * @param ...
- UVA 10881 Piotr's Ants(等效变换 sort结构体排序)
Piotr's AntsTime Limit: 2 seconds Piotr likes playing with ants. He has n of them on a horizontal po ...
- linux管道学习(二)
int main() { char* pipename = "pipe"; mkfifo(pipename,); int pid = fork(); ) { printf(&quo ...
- stringstream vs sprintf, sscanf.
前言 以前一直认为 stringstream 远不如 sprintf. 近日突然萌发了看看 stirngstream 是不是真的如我想的那么烂 对比 // stringstream. stringst ...
- discuz注册 内部错误
ucenter整合后,在论坛注册时出现 内部错误,无法显示,这是因为ucenter中,某个appid没写入! 可以把ucenter安装包下的utilities/checkappid.php,复制到uc ...
- phpstorm配置Xdebug进行调试PHP教程
运行环境: PHPSTORM版本 : 8.0.1 PHP版本 : 5.6.2 xdebug版本:php_xdebug-2.2.5-5.6-vc11-x86_64.dll ps : php版本和xdeb ...
- S3C2440的LCD虚拟显示测试
一.概述 S3C2440的LCD控制器支持虚拟显示,说的容易理解一点就是,可以显示比实际显示器大的图像.可以这样想象,有一个大的图片,但是显示器(显示串口)比较小,但是我们可以相对于大图片(即大图 ...