ccpc預選賽-1005CaoHaha's staff

CaoHaha's staff

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2089    Accepted Submission(s): 818


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的小方格組成的平面中你一秒可以走一步(小方格的一邊或者對角線),然後給你面積問你最少多少秒能畫出大於或等於這個面具的封閉圖像。

找規律推出公式。

首先我們要在相同時間找出我們能畫出面積最大的圖形。所以在邊長相同的情況下面積越接近正方形面積越大,邊數相同的時候邊長越長越大所以邊長儘量用根號2。

當邊爲1或2的時候不能形成封閉圖像。邊爲三時只有一種解法邊長爲1,1,根號2的三角形,面積爲1/2,向下取整爲0.。設邊有n條。

當n爲偶數時,組成一個邊長儘量相近的矩形。

比如邊長爲16.

4*4=16;3*5=15;2*6=12;1*7=7;

所以公式爲

n/4*(n/4+(n%4)/2)*2;

奇數條邊的時候最優情況是這樣的


所以公式爲 

(n-1)/4*((n-1)/4+((n-1)%4)/2)*2+((n-1)/4+((n-1)%4)/2-0.5;

 所以代碼爲:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
double a[500010];
int main(){
    a[0]=0;a[1]=0;a[2]=0;a[3]=0;
    for(int i=4;i<=100000;i++){
        if(i%2==0){
            int k=i%4;
            k=k/2;
            a[i]=(int)2*(i/4)*(i/4+k);
        }
        else{
            int k=(i-1)%4;
            k=k/2;
            a[i]=(int)a[i-1]+(int)((i-1)/4+k-0.5);
        }
    }
    int t;
    scanf("%d",&t);
    while(t--){
        long long n,x;
        scanf("%lld",&n);
        x=lower_bound(a,a+100000,n)-a;
        printf("%lld\n",x);
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章