题目链接:传送门
CaoHaha’s staff
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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
思路:我们只要找到面积,随边的个数变化的规律即可,从面积为8开始,下面是面积为[1,8】的图形。
从面积为8开始,每增加一条最多增加max-0.5的面积(max为该矩形的最大边长,不过边的单位长度为根号2),每增加两条最多增加max*2的面积。
图如下:
可以思考一下,为啥。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int a[9]= {0,4,4,6,6,7,8,8,8};
int main()
{
int t;
int r1,r2;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
if(n<=8)
printf("%d\n",a[n]);
else
{
int sum2=8;
int chang=2,kuan=2;
long long sun=8;
while(1)
{
int max_=max(chang,kuan);//最大的边的长度
int min_=min(chang,kuan);//最小的边的长度
int ss=sum2-1+max_;//减去0.5与1的效果一样,增加一条边,所能增加的最大面积
int ee=sum2+max_*2;//增加两条边所能增加的最大面积
sun+=1;
if(ss>=n)
break;
sun+=1;
if(ee>=n)
break;
kuan=min_+1;
chang=max_;
sum2=ee;
}
printf("%lld\n",sun);
}
}
return 0;
}