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
按照从下到上,从左到右的每个情况….应该可以看出些规律,原谅我渣渣的画图水平
我是按照每四个一周期,第一个和第三个有联系,第二个和第四个有联系,打一个表然后二分查找就行
#include<bits/stdc++.h>
using namespace std;
using LL=int64_t;
const int INF=0x3f3f3f3f;
const int maxn=1e5+5;
double ans[maxn];
void init() {
memset(ans,0,sizeof(ans));
ans[4]=2,ans[5]=2.5,ans[6]=4;
for(int i=7;i<maxn;i++) {
if(i%4==0) ans[i]=ans[i-2]+i/2;
else if(i%4==1) ans[i]=ans[i-1]+(i-1)/4+0.5;
else if(i%4==2) ans[i]=ans[i-2]+(i-2)/2;
else ans[i]=ans[i-1]+(i-3)/4+0.5;
}
}
int lower_bounds(int x,int y,int v) {
int m;
while(x<y) {
m=(x+y)/2;
if(ans[m]>=v) y=m;
else x=m+1;
}
return x;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
init();
int T;
cin>>T;
while(T--) {
int n;
cin>>n;
int cnt=lower_bounds(0,maxn,n);
cout<<cnt<<endl;
}
return 0;
}