2017中国大学生程序设计竞赛 - 网络选拔赛 1005 HDU 6154 CaoHaha's staff (找规律)

时间:2021-12-18 17:23:51

题目链接

Problem Description

"You shall not pass!"

After shouted out that,the Force Staff appered in CaoHaha's hand.

As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.

But now,his new owner,CaoHaha,is a sorcerers apprentice.He can only use that staff to send things to other place.

Today,Dreamwyy come to CaoHaha.Requesting him send a toy to his new girl friend.It was so far that Dreamwyy can only resort to CaoHaha.

The first step to send something is draw a Magic array on a Magic place.The magic place looks like a coordinate system,and each time you can draw a segments either on cell sides or on cell diagonals.In additional,you need 1 minutes to draw a segments.

If you want to send something ,you need to draw a Magic array which is not smaller than the that.You can make it any deformation,so what really matters is the size of the object.

CaoHaha want to help dreamwyy but his time is valuable(to learn to be just like you),so he want to draw least segments.However,because of his bad math,he needs your help.

Input

The first line contains one integer T(T<=300).The number of toys.

Then T lines each contains one intetger S.The size of the toy(N<=1e9).

Output

Out put T integer in each line ,the least time CaoHaha can send the toy.

Sample Input

5

1

2

3

4

5

Sample Output

4

4

6

6

7

题意:

在坐标系中建立网格,对于一个1×1的小正方形来说,它的四条边和两条对角线可以看作任意的一条边,现在给定一个面积,求围成如此大的面积最少需要多少条边。

分析:

本来打算画个图的,这样看起来比较直观一点,但是这图实在太难画了,就直说一下把。

我们首先根据边数来确定它能围成的最大面积,然后根据面积找到第一个大于等于它的边数。

如果是4条边的话,肯定是由四条对角线构成,面积为2

如果是5条边的话,是在由四条对角线构成的图形的基础上,将其中的一条边展开,换成两条边,面积加上0.5,总面积为2.5

6条边的话,是在由四条对角线构成的图形的基础上,将其中的一条边往上移动一个,加在上另外两条边,面积加上2,总面积为4

7条边的话,将6条边的图像的长边展开,分别由对角线移动到边线上,增加一天对角线封顶,比原来的图像面积加上1.5,总面积为5.5

8条边的话,就是原来6条边的图形,长边向外移动一格,面积增加4,总面积为8

9条边的话,将8变形一条边向外扩,如同4变形变为5变形,面积增加1.5,变为9.5

10条边的话,如同4边形变为6边形,面积增加4,总面积为12

11条边的话,就是将10边形的长边展开,如同6边形变为7边形,面积增加2.5,变为14.5

····

从这里我们就可以发现,它们i安华的规律是四个一个周期,在一个周期里,奇数边满足(首相为1.5,公差为1的等差数列),偶数边满足(首相为4,公差为2的等差数列),(因为我们首先要找到前几项作为基础,所以从边数为8的开始)

代码:

#include <bits/stdc++.h>
using namespace std;
const int MAX=90000;
double area[MAX],f=1.5,p=4;
int cnt;
void makeTable()
{
area[3]=0.5;
area[4]=2;
area[5]=2.5;
area[6]=4;
for(int i=7; i<=MAX; i++)
{
int y=i%4;
if(y==1)
{
area[i]=area[i-1]+f;
f+=1.0;
}
if(y==2)
{
area[i]=area[i-2]+p;
p+=2.0;
}
if(y==3)
{
area[i]=area[i-1]+f;
}
if(y==0)
{
area[i]=area[i-2]+p;
}
}
}
int main()
{
makeTable();
int T;
scanf("%d",&T);
while(T--)
{
double n;
scanf("%lf",&n);
for(int i=3; i<=MAX; i++)
{
if(area[i]>=n)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}