CaoHaha's staff
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 620 Accepted Submission(s): 365
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.
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).
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 13 4 5
Sample Output
4 4 6 6 7
题意:在坐标系中,你可以连长度为sqrt(2)的对角线或者就是单位长度的边,要求最少的边构成面积为n的图形。求这个最小边。
找规律加二分搜索
这个图片是从别人博客上借鉴的
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
double ans[maxn];
int main()
{
int t,n;
memset(ans,0,sizeof(ans));
ans[4]=2;
ans[5]=2.5;
ans[6]=4;
ans[7]=5.5;
for(int i=8; 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;
ans[i]=ans[i-2]+(i-1)/2;
}
else if(i%4==2)
{
ans[i]=ans[i-2]+(i-2)/2;
}
else
{
//ans[i]=ans[i-1]+(i-3)/4+0.5;
ans[i]=ans[i-2]+(i-1)/2;
}
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int x,y;
x=0;
y=maxn;
int m;
while(x<y)//二分法
{
m=(x+y)/2;
if(ans[m]>=n)
{
y=m;
}
else
{
x=m+1;
}
}
printf("%d\n",x);
//printf("%lf\n",ans[9]);
}
return 0;
}
这道题很奇怪,面积为10的时候答案为9和10 都可以过!