hdu6154CaoHaha'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

思路:
如下圖所示,首先我們應該優先選擇菱形擴展,先在其一側可以通過添加一條邊,這樣就可以在其這一側增加的面積如斜線的陰影所示。當我們擴展的邊數大於等於二的時候,就會出現圖中最上方標有螺旋狀的小三角形,這樣將旁邊的兩條邊拉過來就可以將面積再增加1;這樣最多擴展四次就可以找到解。邊數較小的情況需要特判。
擴展圖解

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long n;
        cin>>n;
        if(n==1||n==2)
            puts("4");
        else if(n==3||n==4)
            puts("6");
        else if(n==5)
            puts("7");
        else if(n>=6&&n<=8)
            puts("8");
        else
        {
            long long m=floor(sqrt(n/2));
            // printf("%d\n",m);
            long long tmp=m*4;
            long long x=m*m*2;
            if(n==x)
                cout<< tmp<<endl;
            else if(n<=x+m-0.5)
                cout<< tmp+1<<endl;
            else if(n<=x+2*m)
                cout<< tmp+2<<endl;
            else if(n<=3*m+0.5+x)
                cout<< tmp+3<<endl;
            else
                cout<< tmp+4<<endl;
        }
    }
    return 0;
}
發佈了174 篇原創文章 · 獲贊 64 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章