2017ccpc网络赛CaoHaha's staff

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


【题意】

t组样例。

给你一个n,问在网格上,包围为n的面积最少需要的顶点数。(可以算对角线和与网格线平行的线)

这是样例的解释,红色点的个数就是答案。

2017ccpc网络赛CaoHaha's staff_第1张图片

首先,我们反过来求当有m个点的时候,最大包围面积是多少。

队友粗暴的下了判断,当点数为4k时,一定是菱形面积最大。

多一个点。

2017ccpc网络赛CaoHaha's staff_第2张图片

多两个点。嗨呀我的图画歪了。

2017ccpc网络赛CaoHaha's staff_第3张图片

多三个点。

2017ccpc网络赛CaoHaha's staff_第4张图片

多四个点。诶呀图真的好丑。

2017ccpc网络赛CaoHaha's staff_第5张图片

就又变成菱形了。

点数从4开始,算最大包围面积,10^5的个点围的面积就可以超过10^9。所以开个10^5的数组,下标为点数,存面积。

lower_bound找到下标就是答案啦。

下面是代码。

#include
#include
using namespace std;
typedef long long ll;
ll a[100005];
int main(){
	a[0]=a[1]=a[2]=a[3]=0;
	for(int i=4;i<=100005;i++){
		if(i%4==0)	a[i]=2*(i/4)*(i/4);
		else if(i%4==1)	a[i]=a[i-1]+(i/4)-1;
		else if(i%4==2)	a[i]=a[i-2]+(i/4)*2;
		else a[i]=a[i-1]+(i/4);
	}
	//	printf("s=%d\n",a[100000]);
	
	
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		int ans=lower_bound(a+1,a+100000,n)-a;
		printf("%d\n",ans);
	}
	return 0;
}





你可能感兴趣的:(找规律)