题目描述

«问题描述:

假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球。

(1)每次只能在某根柱子的最上面放球。

(2)在同一根柱子中,任何2个相邻球的编号之和为完全平方数。

试设计一个算法,计算出在n根柱子上最多能放多少个球。例如,在4 根柱子上最多可放11 个球。

«编程任务:

对于给定的n,计算在n根柱子上最多能放多少个球。

输入输出格式

输入格式:

第1 行有1个正整数n,表示柱子数。

输出格式:

程序运行结束时,将n 根柱子上最多能放的球数以及相应的放置方案输出。文件的第一行是球数。接下来的n行,每行是一根柱子上的球的编号。

输入输出样例

输入样例#1:

4
输出样例#1:

11
1 8
2 7 9
3 6 10
4 5 11

说明

感谢 @PhoenixEclipse 提供spj

思路:

  因为有数据范围,我们采取二分答案(枚举);

  然后,走最大流,当当前球数减去最大流数==n+1时,当前球数-1就是答案;

来,上代码:

#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 4000 using namespace std; struct EdgeType {
int to,next,flow;
};
struct EdgeType edge[]; int if_z,n,mid,l,r,cnt,head[maxn],ans;
int s=,t=maxn-,deep[maxn],next[maxn]; bool if_[maxn]; char Cget; inline void in(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} inline void edge_add(int u,int v,int w)
{
edge[++cnt].to=v,edge[cnt].next=head[u],edge[cnt].flow=w,head[u]=cnt;
edge[++cnt].to=u,edge[cnt].next=head[v],edge[cnt].flow=,head[v]=cnt;
} bool BFS()
{
memset(deep,-,sizeof(deep));
queue<int>que;que.push(s);deep[s]=;
while(!que.empty())
{
int pos=que.front();que.pop();
for(int i=head[pos];i;i=edge[i].next)
{
if(edge[i].flow>&&deep[edge[i].to]<)
{
deep[edge[i].to]=deep[pos]+;
if(edge[i].to==t) return true;
que.push(edge[i].to);
}
}
}
return false;
} int flowing(int now,int flow)
{
if(flow==||now==t) return flow;
int oldflow=;
for(int i=head[now];i;i=edge[i].next)
{
if(deep[edge[i].to]!=deep[now]+||edge[i].flow==) continue;
int pos=flowing(edge[i].to,min(flow,edge[i].flow));
if(edge[i].to>mid)
{
next[now]=edge[i].to-mid;
if_[next[now]]=true;
}
flow-=pos;
oldflow+=pos;
edge[i].flow-=pos;
edge[i^].flow+=pos;
if(flow==) return oldflow;
}
return oldflow;
} bool check()
{
cnt=;
memset(next,,sizeof(next));
memset(head,,sizeof(head));
memset(if_,false,sizeof(if_));
for(int i=;i<=mid;i++)
{
for(int j=i+;j<=mid;j++)
{
int tmp=sqrt(i+j);
if(tmp*tmp==i+j) edge_add(i,j+mid,);
}
}
for(int i=;i<=mid;i++)
{
edge_add(s,i,);
edge_add(i+mid,t,);
}
int pos=mid;
while(BFS()) pos-=flowing(s,0x7ffffff);
if(pos>n) return true;
else return false;
} int main()
{
in(n);
l=,r=;
while(l<=r)
{
mid=(l+r)>>;
if(check()) ans=mid,r=mid-;
else l=mid+;
}
mid=ans-;
check();
printf("%d\n",mid);
for(int i=;i<mid;i++)
{
if(if_[i]) continue;
printf("%d",i);
int pos=i;
while(next[pos])
{
printf(" %d",next[pos]);
pos=next[pos];
}
printf("\n");
}
return ;
}

最新文章

  1. Difficult Melody(映射)
  2. 第8章 文件系统管理(2)_挂载、fdisk分区及分配swap分区
  3. jQuery打印插件jqprint
  4. http 上传文件
  5. 洛谷 P1015 回文数 Label:续命模拟QAQ
  6. c#生成缩略图
  7. Unity3d:使用uWebKit插件嵌入网页,网页中的flv视频无法播放
  8. IOS APP IDs
  9. Linux常用命令及使用技巧
  10. RVDS 3.1 下载地址及破解方法
  11. 如何在Shell中快速切换目录
  12. Cocos Creator—优化首页打开速度
  13. gRPC 如何使用python表示多维数组
  14. Java开发笔记(三十)大小数BigDecimal
  15. android上instant app介绍 类似于微信小程序
  16. Windows 安装配置MongoDB
  17. http/https协议
  18. 关于@Param
  19. NSFileManager和NSFileHandler的作用, category的理解
  20. POJ 3181 Dollar Dayz 【完全背包】

热门文章

  1. shell 练习题 - 第三周
  2. pandas按索引插入对应值的处理方法 - join
  3. Python基础——类
  4. 这一千个Python库,总有你想要的!
  5. Python并发编程之多进程(实战)
  6. CentOS 7.0 使用 yum 安装 MariaDB 及 简单配置
  7. Latex:插入伪代码
  8. 大家好,我是一个JAVA初学者,想在这里记下自己学习过程中的点点滴滴,请多多关照
  9. Ubuntu关机与重启的相关指令
  10. No identifier specified for entity: XXXX 错误