HDU 6154 CaoHaha's staff(思维+找规律)

来源:互联网 发布:宁波大学网络注销 编辑:程序博客网 时间:2024/06/08 20:16

CaoHaha's staff

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


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
512345
 

Sample Output
44667
 
题意:给你一个整数面积n,要求在单位为1的正方形网格中围出面积大于等于n的封闭图形,图形的边只能是网格中的正方体的边或斜边
思路:要使面积尽量的大我们要有尽量多的斜边,那么我们可以从斜边正方形的基础上来加边看面积的变化,如当有4条边时,能围成的最大面积为这样的斜边正方形,然后考虑加一条边时的情况黑色斜边变为红边,黄边为增加的边,那么面积就在原来的基础上增加了0.5然后考虑再增加一条边,我们进行同样的操作面积增加0.5,但是这并不是最大的面积,我们进行这样的操作把增加的两条直边变为斜边,这样使得面积增加了0.5+1,最后我们增加了4条边后重复操作图形就会变成这样,也就是说是边数为8时面积的最大情况了,这样的斜边正方形都是4的倍数4,8,12,16,这些都可以看作基底,我们找n在什么范围内时首先要找到n所对应的图形是由怎么样的斜边正方形变化而来的,我们知道当斜边数为m个单位对角线的正多边形时,面积为s=m*m*2,所以面积n所对应的正方形斜边数应该是m=sqrt(n/2)个单位对角线,那么我们只要枚举一下加一条边的面积,加一条边的面积就知道n是在几条边的图形里了,加几条边增加的面积为(m-0.5)*i+(i-1)=(m+0.5)*i-1
对于这个公式再解释一下当m=2时,加一条边增加的面积为((m√2+(m-1)√2)*√2/2)/2=m-0.5,就是梯形的面积计算公式m=3,m=4,都是一样的,i-1就是上面的说的加的边上翻可以再多增加1的面积,加i条边就是增加i-1咯,所以一起就是(m-0.5)*i+(i-1)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <math.h>using namespace std;int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        int m=sqrt(n/2);        if(n==m*m*2)        {            printf("%d\n",m*4);            continue;        }        for(int i=1;i<=4;i++)            if(m*m*2+(m-0.5)*i+(i-1)>=n)                {                    printf("%d\n",m*4+i);                    break;                }    }    return 0;}



原创粉丝点击