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);
    }
}




猜你喜欢

转载自blog.csdn.net/qibage/article/details/77427278